如果页面被添加了书签,为什么php header()的行为会很奇怪?
我写了一个php代码。地址栏的动作很神秘。有人能解释发生了什么事吗 index.php如果页面被添加了书签,为什么php header()的行为会很奇怪?,php,Php,我写了一个php代码。地址栏的动作很神秘。有人能解释发生了什么事吗 index.php <?php /* Notes: * $_COOKIE['user_id'] is set and alive * $_SESSION['user_id'] does not exist * session_start() is given already */ ... session_start(); if(!isset($_SESSION['user_id') && isse
<?php
/* Notes:
* $_COOKIE['user_id'] is set and alive
* $_SESSION['user_id'] does not exist
* session_start() is given already
*/
...
session_start();
if(!isset($_SESSION['user_id') && isset($_COOKIE['user_id'])) {
$location = 'Location:http://localhost/project/login.php';
header($location);
}
?>
在地址栏中输入“localhost/project/index.php”。它很好用
我将“localhost/project/index.php”作为书签,这次我使用书签转到index.php。我希望被重定向到“localhost/project/login.php”
但是,浏览器将我重定向到“localhost/project/project/login.php”
有人能给我解释一下这种现象吗
谢谢上帝,
谢谢你的回复。
我发现了问题。不幸的是,这是一个简单的编码错误,需要很长时间才能找到
这就是我的实际代码:
<?php
define('HOST',"http://{$_SERVER['HTTP_HOST']}/project");
session_start();
if(!isset($_SESSION['user_id') && isset($_COOKIE['user_id'])) {
$location = 'Location:'.HOST.$_SERVER['PHP_SELF'];
header($location);
exit();
}
?>
这是您的完整脚本?你有重写规则吗?您是否注释掉了标题
以确认您输入了该条件?您好,Chris85,问题就出在这个if()语句中。正如我提到的,$\u SESSION['user\u id']已死亡$_COOKIE['user_id']仍然存在,并且处于活动状态。你评论标题是什么意思???/*标题($location);*/死亡(“我们在调节”)代码>快速提示:始终使用出口跟踪标题(位置)调用,否则脚本将继续执行,即使您没有看到它。这可能会造成安全问题或模糊的bug。谢谢你的提醒Erik。我只是没有为此代码执行此操作,因为在头之后没有任何内容。:)但为了避免混淆,我将对其进行编辑。
<?php
echo $location;
/*Output: http://localhost/project*/
?>
<?php
echo $location;
/*Output: http://localhost/project/project/index.php*/
?>