Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will PHP script be executed after header redirect?

Tags:

php

Yes, this question has been asked before, however, the answers have been inconsistent. Take Why I have to call 'exit' after redirection through header('Location..') in PHP? for instance. Every answer (including the accepted answer) states yes, except the last answer which received zero votes which says "maybe". I am starting to think the correct answer is "maybe". To make it a simple "yes" or "no" question, will doThis() be executed given the following script? Thanks

header('Location: http://somewhereElse.com'); //die(); sleep(1000); doThis(); 

EDIT Thanks, all. With my PHP/Linux/Apache configuration, the second syslog() executes, so the answer is "yes, all script down stream of the header will be executed." I will assume (and hope I am correct) it is the same with all PHP/Linux/Apache configurations!

<?php   header('Location: http://google.com');   syslog(LOG_INFO,'first');     sleep(5);   syslog(LOG_INFO,'Second');   ?> 
like image 491
user1032531 Avatar asked Feb 13 '13 05:02

user1032531


People also ask

Should I use exit after header in PHP?

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect. exit always interrupts the current script (in your case "fileA").

How does redirect work in PHP?

Redirection in PHP can be done using the header() function. To setup, a simple redirect simply creates an index. php file in the directory you wish to redirect from with the following content: <?

How does header PHP work?

The header() function in PHP sends a raw HTTP header to a client or browser. Before HTML, XML, JSON, or other output is given to a browser or client, the server sends raw data as header information with the request (particularly HTTP Request).


1 Answers

Yes, the script continues to process after the call to header('Location: http://google.com') if you don't explicitly terminate it! I just tried this locally. I added test.php to a site in apache with these contents:

<?php  header('Location: http://google.com'); error_log("WE MADE IT HERE SOMEHOW");  ?> 

And checked my /var/log/apache2/error_log for this entry:

[Tue Feb 12 23:39:23 2013] [error] [client 127.0.0.1] WE MADE IT HERE SOMEHOW 

Possibly surprising, but yes, it continues to execute if you don't halt execution.

like image 74
Cody A. Ray Avatar answered Sep 19 '22 23:09

Cody A. Ray