machiavelli1079 Posted August 5, 2009 Share Posted August 5, 2009 I only posted this here because the forum involves bugs and I'm hoping some of the more experience phpers can explain whether or not the following is a bug. When I execute the following code: <?php $db=mysql_connect("db","username","password"); header("Location: http://www.google.com"); mysql_close($db); header("Location: http://www.msnbc.com"); ?> I get redirected to msnbc. Why does it skip the first header statement, or why does the first header statement fail? Bug? Or am I misunderstanding how the header statement works? Thanks (and sorry if this is wrong board), b Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2009 Share Posted August 5, 2009 Headers are sent to the browser. It is the browser that is performing the request for the new page. When multiple location headers are sent, the browser ends up requesting the URL of the last one it receives last and that is the page you end up on. In the code you posted, php executes through to the last statement in the file because there is no php code that stops or modifies the execution path through that code. You need to put exit/die statements after every header() redirect in order to prevent the remainder of the code on that page from being executed. Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/#findComment-891755 Share on other sites More sharing options...
machiavelli1079 Posted August 5, 2009 Author Share Posted August 5, 2009 I thought as soon as the header("Location: blah"); statement was executed the redirect was executed. In other words, as soon as the header location to google is hit in the php process, why wouldn't the redirect occur then? Is what you are saying that after every header() call I make I should use an exit() code to process the redirect? Is that documented anywhere? Again, to be clear, you are saying that the headers are not sent until all of the php code is executed or the php code is terminated with a die() or exit()? I appreciate your quick response, and apologize for posting in the wrong forum - thanks, b Headers are sent to the browser. It is the browser that is performing the request for the new page. When multiple location headers are sent, the browser ends up requesting the URL of the last one it receives last and that is the page you end up on. In the code you posted, php executes through to the last statement in the file because there is no php code that stops or modifies the execution path through that code. You need to put exit/die statements after every header() redirect in order to prevent the remainder of the code on that page from being executed. Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/#findComment-891758 Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2009 Share Posted August 5, 2009 Headers would be sent when the first character content is output or the script ends. Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/#findComment-891760 Share on other sites More sharing options...
machiavelli1079 Posted August 6, 2009 Author Share Posted August 6, 2009 my other post here explains the root of this post: http://www.phpfreaks.com/forums/index.php/topic,263558.0.html? basically, i call a location header if the session times out. but the script continues to execute, causing an error further along in the processing that produces another header request to an error page. so every time a user times out, the script redirects to an error page instead of the timeout page. This acts in accordance with the fac tyou just stated (that the last header request received before the script terminates gets executed). UNLESS i run mysql_close($db) prior to the first header request. I add nothing else to the code, just a mysql_close statement before the first header location request. Now a timed out user gets sent to the timeout page, even though no content has been output. Now I am 100% sure that if the script continued (as it did in the aforementioned scenario without the mysql_close), the same error would get thrown and the second header request would still be reached in the script. This acts contrary to the fact that headers are sent upon either first output or script termination. The script doesn't output anything with the inclusion of mysql_close($db), and it does not terminate on that command either. Any insight? I feel like I am so close to an answer - i appreciate you taking the time, and would be happy to provide a large segment of the code I have written to determine the cause of this. thanks, b Headers would be sent when the first character content is output or the script ends. Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/#findComment-891766 Share on other sites More sharing options...
machiavelli1079 Posted August 6, 2009 Author Share Posted August 6, 2009 FYI: Using the helpful information provided by numerous users, I used a combination of log files and headers_sent to complete the troubleshooting process. Turns out that calling mysql_close before the header() redirect was not the solution for the reason I originally thought. What happened is that a subsequent call to mysql_real_escape produced an error, which in turn sent the headers with the correct redirect information. Thanks to everyone that helped with this issue. Much appreciated, b Link to comment https://forums.phpfreaks.com/topic/169016-solved-bug-or-misunderstanding/#findComment-891813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.