[英]user authentication using sessions causes redirect loop
我正在尝试制作身份验证脚本,以将已登录/未登录的用户重定向到正确的页面,但这会导致重定向循环。 我有两个页面index.php具有登录表单,而mobile.php具有有效用户的内容。 为了进行身份验证,我在index.php的第一行上有此代码
session_start();
if (isset($_SESSION["username"]) && isset($_SESSION["userid"])) {
header("location: mobile.php");
exit();
}
而mobile.php的第一行是
session_start();
if(!isset($_SESSION["username"])){
header("location: index.php");
}
但我不知道为什么会导致重定向循环。 这是一些屏幕截图。 谢谢你的时间。
这是来自index.php的一些代码,这些代码行后跟用于登录的形式。 在validate函数中,我要做的就是检查用户名和密码是否有效,如果是,则重新生成会话ID,并设置会话变量username和userid。 并返回true。
if(isset($username) && isset($password)){
$username = preg_replace('/[^a-zA-Z0-9]/', '', $username);
$password = preg_replace('/[^a-zA-Z0-9]/', '', $password);
$returnmsg = validate($username,$password);
if($returnmsg===true){
// header("location: mobile.php");
exit;
} else {
$returnmsg = 'Invalid username or password.';
}
}
在位置标头之后始终退出。
session_start();
if(!isset($_SESSION["username"])){
header("location: index.php");
exit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.