PHP - Chrome 下的重定向
我对以下代码有问题:
<?php
session_start();
require_once("config.php");
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
$url = ABS_PATH . $location;
unset($_SESSION['location']);
header('Location: ' . $url);
}
$url 的值是:
http://www.domain.eu/somepage
传递给 header() 的值是:
Location: http://www.domain.eu/somepage
在 Opera、IE (7,8,9) 和 Firefox 下没问题,但不幸的是它在 Chrome 和我不知道为什么。提到的代码示例来自文件index.php,每次加载页面时都会执行它。我有其他文件(display.php),我通过 AJAX 请求向其发送 url。文件display.php设置$_SESSION['location']的值并返回页面内容。在 Chrome 以外的浏览器中,当我刷新页面时,它会将我重定向到正确的 URL。在调试过程中我注意到即使我把 die(); 之后
unset($_SESSION['location']);
在它不执行 ,但是当我把它放在它之前时。谁能知道如何解决我的问题吗?
预先感谢您的帮助。
I have a problem with the following code:
<?php
session_start();
require_once("config.php");
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
$location = $_SESSION['location'];
$url = ABS_PATH . $location;
unset($_SESSION['location']);
header('Location: ' . $url);
}
The value of $url is:
http://www.domain.eu/somepage
and value passed to header() is:
Location: http://www.domain.eu/somepage
It is ok under Opera, IE (7,8,9) and Firefox but unfortunately it is not working under Chrome and I have no idea why. Mentioned code sample comes from file index.php and it is execuded every time You load a page. I have other file (display.php) to which I send url via AJAX request. File display.php sets value of $_SESSION['location'] and returns content of page. In browsers other than Chrome when I refresh page it redirects me to proper URL. During debuging I noticed that even when I put die(); after
unset($_SESSION['location']);
it does not execute it, but when i put it before it works. Can anyone have any idea how to solve my problem?
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我有用。展开该代码并查看它何时损坏。
Works for me. Expand on that code and see when it breaks.
在
header:location
之后添加一个die;
。这应该可以解决问题。Add a
die;
right after yourheader:location
. This should do the trick.Chrome 首先需要一个 http 状态:
Chrome needs an http status first: