Pawan_Agarwal Posted July 25, 2013 Share Posted July 25, 2013 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\index.php:17) in C:\xampp\htdocs\header.php on line 143 I am getting sick of this "warning", please help me and tell me how to remove the warning........ Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/ Share on other sites More sharing options...
requinix Posted July 25, 2013 Share Posted July 25, 2013 Now that you're in the PHP Coding Help forum, README: PHP Resources & FAQs 3. I'm getting a "headers already sent error". What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442061 Share on other sites More sharing options...
Pawan_Agarwal Posted July 25, 2013 Author Share Posted July 25, 2013 thanks for quick reply, please tell me how to re-order the code in php ?? Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442063 Share on other sites More sharing options...
Muddy_Funster Posted July 25, 2013 Share Posted July 25, 2013 thanks for quick reply, please tell me how to re-order the code in php ?? What an utterly stupid request when you have, infact, not provided any code for anyone to re-order! Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442066 Share on other sites More sharing options...
Pawan_Agarwal Posted July 25, 2013 Author Share Posted July 25, 2013 (edited) <?php setcookie("_user_name", "Guest", time()+5); global $total; error_reporting(E_ALL); date_default_timezone_set('Asia/Kolkata'); $username = "*************"; $password = "*************"; $hostname = "*************"; $database = "*************"; $connect = mysql_connect($hostname, $username, $password) or die("Could not connect: ".mysql_error()); $selected = mysql_select_db($database,$connect) or die("Could not select database"); $result = mysql_query("SELECT * from visitors"); if(!$result) {echo(die("cannot execute:").mysql.error());} //fetch tha data from the database while ($row = mysql_fetch_array($result)) { $total=$row['TOTAL']; } if(!isset($_COOKIE["_user_name"])) { global $total; $total=$total+1; $result = mysql_query("UPDATE visitors SET TOTAL='$total'") ; } mysql_close($connect); ?> Edited July 25, 2013 by Pawan_Agarwal Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442105 Share on other sites More sharing options...
Muddy_Funster Posted July 25, 2013 Share Posted July 25, 2013 that's not the code from your OP - there is no header() call anywhere Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442114 Share on other sites More sharing options...
Psycho Posted July 25, 2013 Share Posted July 25, 2013 I can't find your previous post, but the solution is to simply not output anything to the page until you are done processing. Here is a simple example: Wrong way: <head> <title>Test Page</title> </head> <body> <?php if(isset($_SESSION['username'])) { echo "Hello {$_SESSION['username']}"; } else { header("Location: login.php"); } echo "Today is " . date('m-d-Y'); ?> </body> </html> Right way <?php if(isset($_SESSION['username'])) { $output = "Hello {$_SESSION['username']}"; } else { header("Location: login.php"); } $output .= "Today is " . date('m-d-Y'); ?> <head> <title>Test Page</title> </head> <body> <?php echo $output; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442119 Share on other sites More sharing options...
Pawan_Agarwal Posted July 25, 2013 Author Share Posted July 25, 2013 Thanks "Psycho", you helped me with this and it is working now !! Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442156 Share on other sites More sharing options...
Pawan_Agarwal Posted July 25, 2013 Author Share Posted July 25, 2013 (edited) I am still facing the error Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\index.php:17) in C:\xampp\htdocs\header.php on line 42Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\index.php:17) in C:\xampp\htdocs\header.php on line 42 If I write {session_start();} in my code that it gives error here and if I don't write [session_start();], it does not work well........ Edited July 25, 2013 by Pawan_Agarwal Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442157 Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 that's not the code from your OP - there is no header() call anywhere setcookie() sends a header. Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442159 Share on other sites More sharing options...
DavidAM Posted July 25, 2013 Share Posted July 25, 2013 setcookie() sends a header. True, but it is not on line 143. @Pawan_Agarwal Read the error message it tells you exactly where the error occurred and exactly where the output started (the reason for the error). That is the same "output started at" as your original post. Figure out why you have output on line 17 of index.php and fix it (or post it -in code tags- so we can help. Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\index.php:17) in C:\xampp\htdocs\header.php on line 42 Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442161 Share on other sites More sharing options...
AbraCadaver Posted July 25, 2013 Share Posted July 25, 2013 True, but it is not on line 143. Nor, 17, but there is really no telling at this point. We will maybe find out in a post on page 2 or 3. Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442163 Share on other sites More sharing options...
Pawan_Agarwal Posted July 26, 2013 Author Share Posted July 26, 2013 The code is running fine on /practice/visits.php...........but when I merge the code in my website then it is displaying error or warning ..............any ideas what is causing the problem ???? Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442233 Share on other sites More sharing options...
Pawan_Agarwal Posted July 26, 2013 Author Share Posted July 26, 2013 I am using firefox to build the website, I am trying to perform cookies and session operations on separate pages, but when I try to perform the on my website then it give the error message, can any one tell me what is the problem with the code, I don;t understand that the same piece of code is working fine at other place but when I try to merge it with the website, it does not work at all......................hello admin , where are you, will you suggest something here ?> Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442269 Share on other sites More sharing options...
sijugeorge Posted July 26, 2013 Share Posted July 26, 2013 Cookies are sent in the headers of the transmission of the HTTP page. Once you give some output, you cannot modify these anymore. The problem in your case lies in you outputting some of the HTML-document before trying to set the cookie. There are a few ways to solve it; one of which is setting the cookie prior to outputting anything on the page like so <?php$value = 'something from somewhere';setcookie("TestCookie", $value);?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title></head><body></body></html> Alternatively, you could buffer your output so that nothing gets written until you explicitly tell it to <?phpob_start(); // Initiate the output buffer?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title></head><body><?php$value = 'something from somewhere';setcookie("TestCookie", $value);?></body></html><?phpob_end_flush(); // Flush the output from the buffer?>Hope you have understood the point. Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442271 Share on other sites More sharing options...
Psycho Posted July 26, 2013 Share Posted July 26, 2013 (edited) I don;t understand that the same piece of code is working fine at other place but when I try to merge it with the website, it does not work at all......................hello admin , where are you, will you suggest something here ? Admin? The "Admin" is responsible for administrating the forums - not for answering your questions. Everyone here is a volunteer and we respond out of our own willingness to do so. You are not entitled to a response and being insistent is a sure way to encourage people NOT to respond. You were already given an explanation of what causes the header error you initially reported. Your last report stated you are getting "errors or warning" without clarifying which you are getting, whether they are the same header() error as before, or a different one. But, assuming you are getting the same type of header error - we already told you what causes that. You cannot output any content before sending any header commands. The error should state what line number where this is happening. You need to find that line and rework your logic so that line can be executed before you output any content. I already gave you a method of doing that. Put all of your logic at the top of the page. Do NOT use any echo statements or have any content outside of PHP tags. For any output that you need that code to create - have it stored in a variable. Then, after all of the logic of the page has completed, then start your output and use the variables to echo the variable content to the page. As for why you may not get errors in one environment and do in another, consider this: echo "Some string"; if(!isset($_COOKIE['foo'])) { header("Location somepage.php"); } else { //Do something else } If you had already set that cookie while testing in your dev environment you would not trigger the header() command. But, when you move the code to another environment, that cookie would not be set for that environment and the header() command would be attempted - and produce the error. This would mean 1) You did not take the instructions provided previously to fix all the related errors - only the ones you were seeing at the time, and 2) you are not testing your code appropriately. You need to test every "branch" of your code. Anywhere there is an if/else, switch(), or any logic that would do something different in different situations, you need to simulate the conditions to test all of those branches. Edited July 26, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442278 Share on other sites More sharing options...
Pawan_Agarwal Posted July 26, 2013 Author Share Posted July 26, 2013 (edited) <?php ob_start(); global $total; error_reporting(E_ALL); $username = "**********************"; $password = "**********************"; $hostname = "**********************"; $database = "**********************"; $connect = mysql_connect($hostname, $username, $password, $database) or die("Could not connect: ".mysql_error()); $selected = mysql_select_db($database,$connect) or die("Could not select database"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $result = mysql_query("SELECT * from visitors"); if(!$result) {echo(die("cannot execute:").mysql.error());} //fetch tha data from the database while ($row = mysql_fetch_array($result)) { $total=$row['TOTAL']; } if(!isset($_SESSION['__name__'])) { global $total; $_SESSION['__name__']="Guest"; $total=$total+1; $result = mysql_query("UPDATE visitors SET TOTAL='$total'") ; } ob_end_flush(); ?> This the final code that I am trying to execute, please rectify this and tell me if you can fix this. Thanks everyone for supporting the thread.........I think that you understand what I am trying to do with the code, I am checking if the session has not been made then create it and update the session value in database, after creating the session, it will not update itself until the session runs out. Edited July 26, 2013 by Pawan_Agarwal Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442279 Share on other sites More sharing options...
DavidAM Posted July 26, 2013 Share Posted July 26, 2013 I am using firefox to build the website,No, you are not. Firefox is a browser for viewing websites. It does not build websites. Since it is just a viewer, it has nothing to do with the PHP code since PHP runs on the server and is done and gone before the browser ever sees it. can any one tell me what is the problem with the code, I don;t understand that the same piece of code is working fine at other place but when I try to merge it with the website, it does not work at allEvery server has its own set of configuration files. There is obviously something different between the two servers. You need to have error_reporting set to E_ALL and display_errors set to on in development. You also need to be sure that output_buffering is OFF. These are some of the settings that may be different between the two servers that would allow the code to "work" on one and not the other. ...hello admin , where are you, will you suggest something here ?>As Psycho said, we have suggested "something" already to address the specific problems you have posted. This the final code that I am trying to execute, please rectify this"rectify this"? Not going to happen. Well, unless (possibly) if you pony up some money and pay someone to fix it. We are here to help you help yourself, not to do it for you. and tell me if you can fix this.No, we can't, there are too many things we don't know about your application and database. I can tell you (some of) what is wrong with it: 1. ob_start(): Output buffering is almost never needed. It is usually (and in this case, apparently) used as a band-aid for a "headers already sent" error. You need to fix the root cause of the problem. 2. global: It is almost never a good idea or even necessary to use globals. Besides, you are using it incorrectly, and it is completely NOT needed in the code in this particular post (#17). 3. You are SELECTing everything from every row in the visitors table. Then walking through every row, and setting a scalar variable. At the end of the loop, the value ($total) will be the value from the last row. Which may or may not be the row you are trying to pull from. This is a terrible waste of resources. Select ONLY the column(s) and ONLY the row(s) that you actually need. 4. You are UPDATEing every row in the visitors table with the new total value. Is there only one row in this table, or do you need a WHERE condition on the UPDATE statement (as well as the SELECT statement)? 5. There is no session_start() at the beginning of this script. Unless this script is included from another script that has session_start(), -- which means this is not the final code (at least it is not the complete final code) -- there will be no $_SESSION available so the IF test will always be true, but the assignment will not affect any session. 6. You are using the mysql extension functions in new development. This extension has been deprecated. You should use the mysqli extension for all new development. This the final code ... I am checking if the session has not been made then create it and update the session value in database, after creating the session, it will not update itself until the session runs out. Since this script does not output anything, I don't see how 1) it can be of any use at all; 2) it could be producing any headers errors. If this is an entire file that is included in another script, then there may need to be changes in the other file AS WELL. If this is the entire code it could be reduced to: session_start(); -- database connection code here if (!isset($_SESSION['__name__'])) { $sql = 'UPDATE visitors SET TOTAL = TOTAL + 1'; mysql_query($sql) or die('failed to update: ' . mysql_error()); $_SESSION['__name__'] = 'Guest'; } Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442302 Share on other sites More sharing options...
Pawan_Agarwal Posted July 28, 2013 Author Share Posted July 28, 2013 (edited) ////////////////////////////////////////////////////////////////////////////// if(!isset($_SESSION['__name__'])) { global $total; $_SESSION['__name__']="Guest"; $total=$total+1; $result = mysql_query("UPDATE visitors SET TOTAL='$total'") ; } ////////////////////////////////////////////////////////////////////////////// I am applying this in the code but the if condition is returning true, it is never returning false, what must I do here ????????? Edited July 28, 2013 by Pawan_Agarwal Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442508 Share on other sites More sharing options...
DavidAM Posted July 29, 2013 Share Posted July 29, 2013 5. There is no session_start() at the beginning of this script. Unless this script is included from another script that has session_start(), -- which means this is not the final code (at least it is not the complete final code) -- there will be no $_SESSION available so the IF test will always be true, but the assignment will not affect any session. Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442614 Share on other sites More sharing options...
Pawan_Agarwal Posted July 30, 2013 Author Share Posted July 30, 2013 (edited) the code is executing fine for the first execution, but after that it is displaying the warning........... Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\index.php:13) in C:\xampp\htdocs\index.php on line 3 I understand now that in code, i was calling the session_start() at each page, that was the point that i was facing issue a lot, know I have transferred the code and it will be called only once. But , what is happening now is that that single page can be called frequently, so what do I do now if that page is visited by any customer more that once, it will start displaying the warning again and that warning is Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\index.php:13) in C:\xampp\htdocs\index.php on line 3 Edited July 30, 2013 by Pawan_Agarwal Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442635 Share on other sites More sharing options...
mac_gyver Posted July 30, 2013 Share Posted July 30, 2013 the errors in this thread (and the thread you created after this one) are about the same problem. only the line number where the output is occurring at (the output is what is causing the error) and the line number where the header() statement is at (this is only the statement that isn't working because of the output that has already been sent) is changing. are you actually reading and understanding the information that the error messages contain or are you just dumping your errors on a forum and expect someone to be able to help you just based on what the error is? you have actually been told/linked to what the problem is, several times - you cannot send any characters (html markup, such as <!DOCTYPE ... are characters) to the browser before a statement that sends a header() to the browser. statements that send a header() are - session_start(), setcookie(), and header(). the solution is to rearrange the logic on your page so that you don't send any characters to the browser before trying to send a header(). the php code that sends the header() must come first, followed by any code that sends any characters to the browser. doing this requires that you understand what each line of your code is doing so that it will still perform the same operations after you rearrange it. if your code produces one of these errors after the first time it has been requested, that indicates you have some conditional logic that is outputting characters, well conditionally. we cannot possibly help you with your code unless you post the code that reproduces the problem. all you have been posting is the error message and small snippets of your code. just seeing the error message and some out of context code does not tell us what your code is up to the point where the output is occurring at or what you would need to change in your code. after you make an attempt at rearranging your logic, if you are still having problems, you need to post the relevant code. the relevant code would be from line 1 up to and including the line in the error where the OUTPUT is listed as being started at. p.s. i notice a global $total; statement in your code. the global keyword only has meaning inside of a function, and even there you should not use it, and i doubt the code you have been posting is inside of a function. you need to remove the global $total; statement. you can also increment a column in a database table using only the UPDATE query. you don't need to select the value first. both of these things were already mentioned by DavidAM in his reply. Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442660 Share on other sites More sharing options...
Pawan_Agarwal Posted July 30, 2013 Author Share Posted July 30, 2013 well, i am trying my best to sort it out, however, I am just stuck in it. I am unable to remove the warning as I am newbie... Just understand that I am not posting the code directly here, I am trying to sort it out 1st and then i post here....I tried to rearrange the code but I did not get it right, I am posting the complete code here...............you try to rearrage it and let me know how to make it work ? The code itself is not making a problem here... I am saving the code in counter.php and this page has been included in index.php page.............if I execute the page counter.php, i get no error or warning, but if i include counter.php in index.php then i get error or warning ............I hope you got my point............ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// <?php session_start(); global $total; error_reporting(E_ALL); $username = "******************"; $password = "******************"; $hostname = "******************"; $database = "******************"; $connect = mysql_connect($hostname, $username, $password, $database) or die("Could not connect: ".mysql_error()); $selected = mysql_select_db($database,$connect) or die("Could not select database"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $result = mysql_query("SELECT * visitors"); if(!$result) {echo(die("cannot execute:").mysql.error());} //fetch tha data from the database while ($row = mysql_fetch_array($result)) { $total=$row['TOTAL']; } if(!isset($_SESSION['__name__'])) { global $total; $_SESSION['__name__']="Guest"; echo $_SESSION['__name__']; $total=$total+1; $result = mysql_query("UPDATE visitors SET TOTAL='$total'") ; } ?> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442698 Share on other sites More sharing options...
Solution Pawan_Agarwal Posted July 31, 2013 Author Solution Share Posted July 31, 2013 I rearrange the code and now it is working fine.............I am thankful to all guys participated in helping me on this post............ Quote Link to comment https://forums.phpfreaks.com/topic/280490-what-is-wrong-with-the-code/#findComment-1442772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.