PHP - Chrome 下的重定向

发布于 2024-12-12 02:53:01 字数 849 浏览 0 评论 0原文

我对以下代码有问题:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美羊羊 2024-12-19 02:53:01
<?
session_start();
$_SESSION['location'] = 'http://www.google.com';
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
    $location = $_SESSION['location'];
    header("location: ".$location);
}
?>

对我有用。展开该代码并查看它何时损坏。

<?
session_start();
$_SESSION['location'] = 'http://www.google.com';
if(isset($_SESSION['location']) && !empty($_SESSION['location'])) {
    $location = $_SESSION['location'];
    header("location: ".$location);
}
?>

Works for me. Expand on that code and see when it breaks.

漆黑的白昼 2024-12-19 02:53:01

header:location 之后添加一个 die;。这应该可以解决问题。

Add a die; right after your header:location. This should do the trick.

晨曦慕雪 2024-12-19 02:53:01

Chrome 首先需要一个 http 状态:

header("Status: 200");
header('Location:' . $url);

Chrome needs an http status first:

header("Status: 200");
header('Location:' . $url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文