PHP - Clear Output To Change Headers?
Is there any way to have html output and then clear the output to allow for headers to be changed? This is still while the php file is executing. The reason I ask is that I am trying to output a progress bar of an excel file being created. So while the file is being created by php, I want to output to the browser a progress bar and then once it is complete, I need to set headers so that it downloads it as an excel file.
Is this possible or does anyone else have any ideas? Should I maybe save the file on the web server temporarily and then redirect to the file afterwards? Any help would be appreciated. Thanks, -JC Similar TutorialsI am having a problem with my login page when I set cookies I get this error. Quote Warning: Cannot modify header information - headers already sent by (output started at /login.php:10) in /loginform.php on line 66 Warning: Cannot modify header information - headers already sent by (output started at /login.php:10) in /loginform.php on line 67 Warning: Cannot modify header information - headers already sent by (output started at /login.php:10) in /loginform.php on line 68 Warning: Cannot modify header information - headers already sent by (output started at /login.php:10) in loginform.php on line 72 I do have ob_start() at the beginning and ob_flush() and the end but i still get the error <?phpinclude('config.php');ob_start();@session_start();if (!isset($_POST['login'])) {?><form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <fieldset> <legend>Login</legend> <table> <tr> <td>Email </td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Password </td> <td><input type="password" name="password" /></td> </tr> <tr> <td><input type="submit" name="login" value="Login" /></td> </tr> <tr> <td><a href="<?=$siteurl?>/register.php">Register</a></td> </tr> </table> </fieldset></form><?php}else{ //Declare some variables $email = secure($_POST['email']); $password = secure($_POST['password']); //Check if the form is filled out if (!$email || !$password) { echo 'Please fill out the form completely'; echo '<a href="'. $siteurl .'/login.php">Go Back</a>'; } else { $user = mysql_query("SELECT * FROM `users` WHERE email = '$email'"); if (! mysql_num_rows($user)) { echo 'That user doe not exist'; } else { //Encrypt the password $encpass = sha1($password . $salt); //Find the user $superquery = mysql_query("SELECT * FROM `users` WHERE email = '$email' AND password = '$encpass'"); if (mysql_num_rows($superquery) == 1) { //If the user is found, set the cookies setcookie("username", $email, 0); setcookie("password", $encpass, 0); setcookie("is_logged_in", true, 0); //send the user to the home page header("Location: " . $siteurl . "members.php"); } else { echo 'Password was incorrect. Please try again.'; } } }}ob_flush();?>() what am i doing wrong? Hey there! I was wondering if it's possible to change the header columns on a CSV export from a MySQL select? Currently it pulls the information that I need and creates a CSV file, but the first row is the column name from the database and I am not sure if there is a way to change that when the CSV is created. Here's the export script that I am using: <?php // Connect database require_once 'library/config.php'; require_once 'library/common.php'; $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "SELECT * FROM tbl_order, tbl_order_item, tbl_product WHERE tbl_order.od_id = tbl_order_item.od_id AND tbl_order_item.pd_id = tbl_product.pd_id AND tbl_order.od_status = 'Paid'"; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while $now = date("m-d-Y"); $output_file = $now."_export-labels.csv"; file_put_contents($output_file,$out); header('Content-Type: application/octet-stream; name="' . $output_file . '"'); header('Content-Disposition: inline; filename="' . $output_file . '"'); readfile($output_file); //header('Content-type: application/csv'); //header('Content-Disposition: attachment; filename="export.csv"'); //readfile('export.csv'); ?> More than anything, I just want to know if it's possible or not. Thanks! Ok so I have this script that I wrote and it works really good. I wanted to display the number of records in a table and since the db only changes once per day I thought I would put the couner on the back end. I have this script that changes the value of the text file every day. But I would love it if it outputted with commas.
Now it reads:
1234070
Want it to read:
1,234,070
Any thoughs?
#!/usr/bin/php -q <?php $username=""; // READONLY USER USED HERE AND AUTH'D ON LOCALHOST ONLY $password=""; $database=""; $dbhost=""; mysql_connect($dbhost,$username,$password); @mysql_select_db($database) or die( "Unable to connect to database please try again!"); $counthd=mysql_query("SELECT count(*) as total from HD"); $hdtotal=mysql_fetch_assoc($counthd); $dbcount = $hdtotal['total']; echo ""; $myFile = "dbcounter.txt"; $fh = fopen($myFile, 'w'); fwrite($fh, "$dbcount"); fclose($fh); ?> Edited by chadrt, 01 February 2015 - 07:26 PM. Hello all, I'm trying to change the way the output look like from Recursion instead of it looking like this and make it look like this here is my code Code: [Select] <?php $user = $_GET['id']; echo '<hr>'; function display_mptt($user) { global $db; $id = $_GET['id']; // retrieve the left and right value of the $root node $sql2 = "SELECT * from mptt where id= ".$id.""; $result2 = mysql_query($sql2 ,$db); if(!$row2 = mysql_fetch_array($result2)) echo mysql_error(); echo '<h1>Your Tree</h1>'; // start with an empty $right stack $right = array(); // now, retrieve all descendants of the $root node $sql = "SELECT * from mptt WHERE `left` BETWEEN ".$row2['left']." AND ".$row2['right']." ORDER BY 'left' ASC"; $result = mysql_query($sql ,$db); // display each row while ($row = mysql_fetch_array($result)) { // only check stack if there is one if (count($right)>0) { // check if we should remove a node from the stack while ($right[count($right)-1]<$row['right']) { array_pop($right); } } // display indented node title // add this node to the stack $right[] = $row['right']; } echo str_repeat(' ',count($right)).$row['title']."<br>"; } display_mptt(1); ?> Dears, I want to output an image by imagepng function in between some texts. For that I used the below codes Code: [Select] // Above this i set image properties by GD functions in $img variable ob_start(); echo "Text 1"; header("Content-type:image/png"); imagepng($img); imagedestroy($img); header("Content-type:text/html"); echo "Text 2"; ob_end_flush(); But unfortunately the image is not visible, rather it turns into some abnormal characters. But if I get rid of the texts (like below), image is printed perfectly. Code: [Select] // Above this i set image properties by GD functions in $img variable ob_start(); header("Content-type:image/png"); imagepng($img); imagedestroy($img); ob_end_flush(); Anybody please help. Well I have a script that executes a scan on a system set to run infinitely, and I need it to echo out a message each time it loops through, but I don't want it to echo out the message with the next loop message below it, and the next one below that etc... I've tried using the flush(); function and been messing around with that with no luck. For security reasons I don't want to release any of the processing code, but here is the basic construction of the script: <?PHP ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $RepeatIt = -1; for($g=1; $g!=$RepeatIt+1; $g++) { ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $ScanMessage = ":.:.: SCANNING THE HITLIST FOR MOBSTER: ".$MobName." (SCAN #$g) :.:.:"."<br/><br/>"; echo $ScanMessage; ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** } ?> At the moment it's returning: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #1) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #2) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #3) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #4) :.:.: So what I want it to do is just delete the scanning message and replace it with the next scan message so while running this script you would see just the number increment on the same line. Any suggestions? Thanks. Okay, I've read all the rules for Headers...still can't it to execute properly. At the bottom is my code. I basically want to check the database, see if the user is already registered, and redirect as appropriate (there is no HTML on my page, no info sent to the browser). My server doesn't send any error messages. All I get are white screens. Now, if I do the headers_sent()...it tells me that it's sent right after the initial connection with the database. The ONLY way I can get it to redirect, is if I put <?php header("location:index.php?msg=3"); ?> at hte very top, but then I can't evalute...which doesn't help me much. Here is the code (obviously, I have more...but it's sending headers in these lines) <?php $connection = mysql_connect("localhost","####","#####"); if (!$connection){ echo mysql_errno().": ".mysql_error()."<br/>"; exit; } if(!mysql_select_db("prixaurora")){ echo("Database not found<br>"); } Hi I am getting the following php error that is on all my pages and it works fine on the other pages except index.php Code: [Select] <?php session_start(); include 'includes/conn_db.php'; if (isset($_POST['submit'])) { if (empty($_POST['userName'])) { $u = FALSE; $nouser = '<font color="red" size="2" face="arial">You forgot to enter your username!</font>'; } else { $u = $_POST['userName']; } if (empty($_POST['userPassword'])) { $p = FALSE; $nopass = '<font color="red" size="2" face="arial">You forgot to enter your password!</font>'; } else { $p = $_POST['userPassword']; } $errorMessage = $nouser . "<br>" . $nopass; if ($u && $p) { $query = "SELECT * from clients WHERE email = '$u' AND pword = '$p'"; $result = mysql_query($query, $conn); while ($row = mysql_fetch_assoc($result)){ if ($u == $row["email"] && $p == $row["pword"]) { $query_name = "SELECT * FROM clients WHERE email = '$u' AND pword = '$p'"; $result_name = @mysql_query ($query_name); while ($row_name = mysql_fetch_assoc($result_name)){ $client_id_out = $row_name['client_id']; $firstname_out = $row_name['firstname']; $lastname_out = $row_name['lastname']; // Create sessions $_SESSION['client_id'] = $client_id_out; $_SESSION['firstname'] = $firstname_out; $_SESSION['lastname'] = $lastname_out; ob_end_clean(); header ("Location: myaccount.php"); exit(); }} } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" /> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <link href="style.css" rel="stylesheet" type="text/css" /> <link href="layout.css" rel="stylesheet" type="text/css" /> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/cufon-yui.js" type="text/javascript"></script> <script src="js/cufon-replace.js" type="text/javascript"></script> <script src="js/Futura_Bk_BT_400.font.js" type="text/javascript"></script> <script src="js/Futura_XBlk_BT_400.font.js" type="text/javascript"></script> <!--[if lt IE 7]> <script type="text/javascript" src="js/ie_png.js"></script> <script type="text/javascript"> ie_png.fix('.png'); </script> <![endif]--> </head> Thanks Lional Hi all, It's probably something obvious, but perhaps someone could explain to me what is going on here... I spent a lot of time this afternoon trying to fix what seemed to be a very odd problem, which involved an image being chucked out like so: Code: [Select] <?php header("Content-Type: image/png"); echo file_get_contents("http://www.example.com/some_image.png"); ?> Except the image was coming out as corrupted, ultimately because I had some white space before this snippet (foolish, I know). The reason it took me so long to diagnose the problem was because I wasn't getting PHP errors (even though they were switched on). As far as the script was concerned, it seems I wasn't sending anything before attempting to modify the headers. After solving the problem, I put the following little bit of code together, trying to force a header error... And yet I get nothing. It all works, and I get redirected, when I'm sure I shouldn't be. I've run this on a default install of XAMPP, and a configured CentOS server, and the same thing happens on both. If anyone can explain to me what is going on, I would be most appreciative! There are some inline comments which should help clarify what I mean. Code: [Select] <?php error_reporting(E_ALL); ini_set("display_errors","1"); ?> <p>Hello! The very existence of this text should really have resulted in the headers being sent... Shouldn't it?</p> <p>So I'd expect to see a "Headers already sent" message... Shouldn't I?</p> <?php // Headers sent? echo "<p>"; echo "Headers "; if(!headers_sent()) { echo "not "; // This "not" does echo out. } echo "sent.</p>"; // This WILL start a session (or at least, won't throw an error) session_start(); // This redirect WILL work, and you will never see the above messages. header("Location: redirect.php"); // What?! ?> Thanks! Dave hello, I'm using this code: <?php if (isset($_REQUEST['email'])) { $name = $_REQUEST['name'] ; $email = $_REQUEST['email'] ; $code = $_REQUEST['code'] ; $from = $email; mail("email@email.com", "Winner request id: $name - code: $code", "From:" . $from); } $headers = "From:" . $from; header('Location: index.php?done'); ?> And I'm getting: Code: [Select] Warning: Cannot modify header information - headers already sent by (output started at /home/site/public_html/config.php:10) in /home/site/public_html/send.php on line 13 This worked before. Many thanks My error: Code: [Select] Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/b/webaskius/htdocs/index.php:7) in /www/zxq.net/w/e/b/webaskius/htdocs/functions.php on line 6 Now, I can't quite figure out why it's doing this. The section of code that is causing this problem is above any HTML tags, at the very beginning of the file. <?php include('functions.php'); ?> <link href="style.css" rel="stylesheet" type="TEXT/CSS"> <img src="logo.png" border="0"> <div id="maincontent"> As you can see, the functions that cause this error are at the VERY beginning of my file. This is where they are called within index.php: if(mysql_num_rows($query_verify_login) > 0) { create_cookie(10000, 'user', $username); redirect('index.php'); } else { echo 'Woops! You\'ve entered in the wrong username and password combination!'; } And finally, my functions: <?php //our cookie creator function function create_cookie($time, $name, $data) { setcookie($name, $data, time()+$time); } function redirect($url) { header('Location: '. $url .''); } function get_replies($id) { //set query to find posts that match our id $query_get_replies = mysql_query("SELECT * FROM replies WHERE to = {$id}"); return mysql_num_rows($query_get_replies); } ?> I have a form as a php include and it's giving me this error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/altimusw/public_html/www.csmsolution.com/register.php:6) in /home/altimusw/public_html/www.csmsolution.com/core.php on line 17 Here is the main page that includes the php include: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>CSM Solution </title> <style type="text/css"> <!-- #Layer1 { position:absolute; left:100px; top:70px; width:800; height:62px; z-index:1; } .style1 { font-family: Arial, Helvetica, sans-serif; color: #535959; font-size: 12px; } --> </style> </head> <body> <div id="Layer1"> <table width="800" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="97"><img src="images/header.jpg" width="800" height="59" border="0" usemap="#Map" /><br /> <img src="images/navbar.jpg" width="800" height="38" border="0" usemap="#Map2" /> <map name="Map2" id="Map2"> <area shape="rect" coords="506,1,552,26" href="contact.php" /><area shape="rect" coords="459,1,502,26" href="form.php" /><area shape="rect" coords="396,-1,455,25" href="login.php" /> <area shape="rect" coords="357,0,393,26" href="index.php" /> </map> </td> </tr> <tr> <td><img src="images/welcome_img.jpg" width="131" height="21" /></td> </tr> <tr> <td><?php include("form.php"); ?></td> </tr> <tr> <td><img src="images/placeholderclouds_bottem.jpg" width="800" height="60" /></td> </tr> </table> </div> </body> </html> Here is the Core.php the error is referring to: <?php //CSM Solution Core. Rahul Parkar 2010 $_config = array(); $_config['db_type'] = "mysql"; $_config['db_server'] = "localhost"; $_config['db_username'] = "altimusw_csm"; $_config['db_password'] = ']17=rXh7tJ0~'; $_config['db_database'] = "altimusw_csm"; $_config['db_tbl_prefix'] = ''; $_config['user_table'] = "customers"; $_config['user_username'] = "username"; $_config['user_password'] = "password"; $_config['user_active'] = "approved"; $_config['user_role1'] = "prirole"; $_config['user_role2'] = "secrole"; $_config['pass_hash_func'] = "SHA1"; session_start(); function fetch_row($query) { build_connection($db); sql_exec($db, $query, $result); sql_fetchone($result, $row); finish_connection($db); return $row; } function build_connection(&$db) { global $_config; if ($_config['db_type'] == "mysql") { $db = mysql_connect($_config['db_server'], $_config['db_username'], $_config['db_password']) or die(mysql_error()); mysql_select_db($_config['db_database'], $db) or die(mysql_error()); } } function sql_exec(&$db, $query, &$result) { global $_config; if ($_config['db_type'] == "mysql") $result = mysql_query($query, $db) or die(mysql_error() . "/$query"); } function sql_fetchone(&$result, &$row) { global $_config; if ($_config['db_type'] == "mysql") { $row = mysql_fetch_assoc($result); @mysql_free_result($result); } } function sql_do(&$db, $query) { global $_config; sql_exec($db, $query, $noclue); finish_connection($db); } function finish_result(&$result) { global $_config; if ($_config['db_type'] == "mysql") @mysql_free_result($result); } function finish_connection(&$db) { global $_config; if ($_config['db_type'] == 'mysql') mysql_close($db); } function check_auth($username, $password) { global $_config; $row = fetch_row("SELECT `" . $_config['user_active'] . "`, `" . $_config['user_password'] . "`, " . $_config['pass_hash_func'] . "('" . $password . "') AS `passcheck`, `fname`, `lname` FROM `" . $_config['user_table'] . "` WHERE `" . $_config['user_username'] . "`='" . $username . "' LIMIT 1"); if (!$row[$_config['user_active']]) { return -1; } else { if ($row[$_config['user_active']] != 1) { return -2; } else if ($row[$_config['user_password']] == $row['passcheck']) { $_SESSION['username'] = $username; $_SESSION['fname'] = $row['fname']; $_SESSION['lname'] = $row['lname']; return 1; } else { return -1; } } } function load_access() { global $_config; if ($_SESSION['username']) { $row = fetch_row("SELECT `" . $_config['user_role1'] . "` FROM `" . $_config['user_table'] . "` WHERE `" . $_config['user_username'] . "`='" . $_SESSION['username'] . "' LIMIT 1"); $role1 = $row[$_config['user_role1']]; $_SESSION['role1'] = $role1; } else { return -1; } } function has_level() { for ($i = 0; $i < func_num_args(); $i++) { if (func_get_arg($i) == $_SESSION['role1']) { return 1; } } return 0; } ?> I am trying to do a page redirect using the header() function.
I am aware that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
and also that reading code with include, or require, functions, or another file access function, can create spaces or empty lines that are output before header() is called. thus causing a redirect failure.
I have therefore used this output control function (ob_start(); ob_implicit_flush() right at the top of my page.
I am then creating the redirect like this as part of my login() function:
if($user_role == ADMIN_LEVEL || $user_role == EDITOR_LEVEL) { if (!headers_sent($filename, $linenum)) { return header("Location:http://mysite.com/dashboard.php"); exit; } else { echo "Headers already sent in $filename on line $linenum\n" . "Cannot redirect\n"; exit; } }Unfortunately I am still getting this error: Headers already sent in /home/steveoje/public_html/login.php on line 1 Cannot redirectAnd this is what I have at the top of the login.php page: <?php ob_start(); ob_implicit_flush(); require_once 'includes/config.php'; if(isset($_POST['submit'])) { login($_POST['username'], $_POST['password']); } require_once "includes/header.inc.php" ?> //other html contentSo why would the redirect fail in this instance? Thanks. Edited by terungwa, 28 September 2014 - 05:28 AM. OK you fantastic PHPfreakers I'm not sure how to describe this so I hope you will understand..
I have a predicament where I set the header info to: header('Content-Type: text/html; charset=utf-8'); but later on I have a part of my site that requests an image from somewhere else using getImage() here I need to set header("Content-type: image/jpeg"); in order for the image to show correctly. but this conflicts with the previous page of headers already sent.
so I was looking for a way around this OR maybe can I load them in after the page has loaded? I have tried using Jquery to post with ajax but I don't seem to get any response the alert does not trigger for any conversation with an image.
I have tested all of this and it works as long as I don't put any OUTPUT before triggering the getImage()
<script type='text/javascript'> $(document).ready(function(){ function loadImages(){ $.ajax({ type: "POST", url: "/admin/getImage.php", data: {token: '<?php echo $token; ?>', chatId: '<?php echo $chat; ?>', uId: '<?php echo $uId; ?>'}, success: function(data){ alert(data); $('#img').html(data); } }); return false; } }); </script>here is part of the messages page that would show the image. (just imagine there was more content etc..) <?php header('Content-Type: text/html; charset=utf-8'); ## OTHER GENERIC STUFF $parser = new Parser($v1, $v2); $convo = $parser->getConvo($token, $id); foreach($convo['messageGroups'] as $con) { $chat = $con['messageGroupId']; $uId = $con['latestMessage']['messageUid']; $key = $con['latestMessage']['messageKind']; if($key == '3'){ $image = $parser->getImage($token, $chat, $uId); echo $image; } } ?>here is the function for getImages() public function getImage($token, $chat, $uId){ $this->initCURL(); $chatter = str_replace("{{chat}}", $chat, $this->url['image']); $chatter = str_replace("{{uId}}", $uId, $chatter); $headerData = $this->defaultGETHeader($token); curl_setopt($this->curl, CURLOPT_URL, $chatter); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headerData); $output = curl_exec($this->curl); $this->closeCURL(); header("Content-type: image/jpeg"); return $output; } Edited by xXREDXIIIXx, 01 June 2014 - 12:18 PM. Hi ! I am currently trying to develop a PHP application in which my server downloads a file and the user can do the same almost simultaneously. I already think about the problem "If the user downloads fastly than the server...", but it's not a problem at this moment. To do so, I used the header and readfile functions of php. Here is my code : Code: [Select] header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.$data['name'].'";'); header('Content-Transfer-Encoding: binary'); header('Content-Length: '.$data['size']); readfile($remoteFile); I must to use the Content-length header to set the proper size of the file and not the size that is downloaded when the user clicks on the link. However, after some seconds or minutes, download is stopped and I need to restart... If you think about a solution, even if it didn't use the header(); function, please tell me. Thank you in advance... Hi;
I want to get the response headers using ob_get_contents ?
For example using file_get_contents, it is easy by accessing the variable $http_response_header like that :
<?php $content = file_get_contents($url); print_r($http_response_header); ?>But how to do that using ob_get_contents without a file_get_contents ? something like for example : <?php ob_start(); include($path_to_script_here); $string = ob_get_contents(); // Access the headers here ob_end_clean(); ?>Thank you Edited by Dareros, 07 October 2014 - 06:20 PM. Hi All, I am trying to use headers at the end of functions to put the user where they need to be. header("location: register.php?user_may_exist"); I understand how they work and their qwuirks. My issue is, in my head file, i am outputting which causes issues for the headers which come after. I have the following in head.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php if(isset($currentPageTitle)){ echo $currentPageTitle; }else{ echo 'Cricket Club'; }; ?> </title> <?php if(isset($currentPageMeta)){ echo "<meta name='description' content='$currentPageMeta'>"; }else{ echo "<meta name='description' content='All the latest from CC'>"; }; ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Is there a better way for me to achieve this? Just a quick question. I'm using the Live HTTP Headers add-on for Firefox that display all the HTTP headers etc. I'm writing an article on it and want to include a screenshot of it in action. Are there any headers that I should hide from the reader for security reasons? Thanks for any help. I'm adding a login page to my website. When I go to the login page, and try to login, I keep getting the error "Warning: Cannot modify header information - headers already sent by (output started at C:\Users\me\Desktop\xampp\htdocs\site\login.php:7) in C:\Users\me\Desktop\xampp\htdocs\site\login.php on line 38" and I don't know why. I've looked into it, and from what I can tell its something to do with white spaces, but I cant find them. <?php include "./dbconnect.php"; ?> <?php forum_connect(); ?> <?php if(isset($_COOKIE['ID_forum'])) { $username = $_COOKIE['ID_forum']; $pass = $_COOKIE['Key_forum']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { if ($pass != $info['password']) { } else { header("Location: main.php"); } } } if (isset($_POST['submit'])) { if(!$_POST['username'] | !$_POST['pass']) { die(' <h2> You did not fill in all of the fields</h2> <p<a href="login.php">Return to login page</a> '); } if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error()); $check2 = mysql_num_rows($check); if ($check2 == 0) { die(' <h2> That user does not exist in our database.<br/> </h2> <p<a href="login.php">Return to login page</a> '); } while($info = mysql_fetch_array( $check )) { $_POST['pass'] = stripslashes($_POST['pass']); $info['password'] = stripslashes($info['password']); $_POST['pass'] = md5($_POST['pass']); if ($_POST['pass'] != $info['password']) { die(' <h2> Incorrect password, please try again</h2> <p<a href="login.php">Return to login page</a> '); } else { $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie(ID_forum, $_POST['username'], $hour); setcookie(Key_forum, $_POST['pass'], $hour); header("Location: main.php"); } } } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <table border="0"> <tr><td colspan=2><h1>Login</h1></td></tr> <tr><td><h2>Username:</h2></td><td> <input type="text" name="username" maxlength="40"> </td></tr> <tr><td><h2>Password:</h2></td><td> <input type="password" name="pass" maxlength="50"> </h2> </td></tr> <tr><td colspan="2" align="right"> <input type="submit" name="submit" value="Login"> </td></tr> </table> </form> <?php } ?> Any help would be great Thanks |