标头重定向后会执行PHP脚本吗? [英] Will PHP script be executed after header redirect?

查看:79
本文介绍了标头重定向后会执行PHP脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,之前曾有人问过这个问题,但是答案却不一致.以为什么我必须叫'exit例如,通过PHP中的标头('Location ..')重定向之后?.每个答案(包括已接受的答案)都表示是,但最后一个答案的票数为零(表示可能")除外.我开始认为正确的答案是也许".为了使它成为一个简单的是"或否"问题,是否会在给出以下脚本的情况下执行doThis()?谢谢

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();

编辑 谢谢大家使用我的PHP/Linux/Apache配置,将执行第二个syslog(),因此答案是是的,将执行头文件的所有下游脚本".我会假设(并希望我是对的)所有PHP/Linux/Apache配置都是相同的!

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');  
?>

推荐答案

是的,如果您未明确终止header('Location: http://google.com'),脚本将继续处理.我只是在本地尝试过.我将test.php添加到具有以下内容的Apache网站中:

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");

?>

并检查我的/var/log/apache2/error_log中的此项:

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.

这篇关于标头重定向后会执行PHP脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆