Php 如何测试用户是否已登录
我对PHP相当熟悉,并希望创建一个登录/注册系统,但我的代码遇到了一个问题。我试图测试用户是否已经登录,以便根据需要重定向他们,但每当我强制登录并转到注册页面时,它不会根据需要重定向我。有人能帮我吗。这是我的密码 Register.phpPhp 如何测试用户是否已登录,php,session,Php,Session,我对PHP相当熟悉,并希望创建一个登录/注册系统,但我的代码遇到了一个问题。我试图测试用户是否已经登录,以便根据需要重定向他们,但每当我强制登录并转到注册页面时,它不会根据需要重定向我。有人能帮我吗。这是我的密码 Register.php <?php if (isset($_SESSION['user_id'])) { header('index.php'); } ?> <!DOCTYPE html&
<?php
if (isset($_SESSION['user_id'])) {
header('index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register Account</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/app.css">
</head>
<body>
<div class="container">
<center><form action="#" method="POST">
<div class="form-group top">
<input type="text" name="firstname" class="form-control width" placeholder="Enter Firstname">
</div>
<div class="form-group">
<input type="text" name="lastname" class="form-control width" placeholder="Enter Lastname">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control width" placeholder="Enter Email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control width" placeholder="Enter Password">
</div>
<div class="form-group">
<input type="password" name="password_confirmation" class="form-control width" placeholder="Confirm Password">
</div>
<input type="submit" name="register-button-validate" class="btn btn-success btn-lg">
</form></center>
</div>
</body>
</html>
<?php
session_start();
$_SESSION['user_id'] = 1;
?>
登记帐户
Index.php
<?php
if (isset($_SESSION['user_id'])) {
header('index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register Account</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/app.css">
</head>
<body>
<div class="container">
<center><form action="#" method="POST">
<div class="form-group top">
<input type="text" name="firstname" class="form-control width" placeholder="Enter Firstname">
</div>
<div class="form-group">
<input type="text" name="lastname" class="form-control width" placeholder="Enter Lastname">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control width" placeholder="Enter Email">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control width" placeholder="Enter Password">
</div>
<div class="form-group">
<input type="password" name="password_confirmation" class="form-control width" placeholder="Confirm Password">
</div>
<input type="submit" name="register-button-validate" class="btn btn-success btn-lg">
</form></center>
</div>
</body>
</html>
<?php
session_start();
$_SESSION['user_id'] = 1;
?>
首先,
register.php
在顶部也需要这个:
session_start();
基本上,任何向会话写入或从会话读取的页面都需要这样做
其次,这不是重定向:
header('index.php');
这是:
header('Location: index.php');
浏览器可能会忽略不完整的标题,因为没有键告诉浏览器如何处理该值。首先,
register.php
在顶部也需要此项:
session_start();
基本上,任何向会话写入或从会话读取的页面都需要这样做
其次,这不是重定向:
header('index.php');
这是:
header('Location: index.php');
不完整的标题可能只是被浏览器忽略了,因为没有键告诉浏览器如何处理该值。是的,这使重定向生效,但我仍然有问题。在index.php中,如果执行session_unset();然后尝试返回register.php我仍然被重定向回index.php。您应该尝试取消设置此会话变量或所有会话变量。因此,请尝试执行unset($\u SESSION['user\u id'])或unset($\u SESSION)。是的,这可以重定向到工作状态,但我仍然有一个问题。在index.php中,如果执行session_unset();然后尝试返回register.php我仍然被重定向回index.php。您应该尝试取消设置此会话变量或所有会话变量。因此,一般来说,可以尝试取消设置($\u会话['user\u id'])或取消设置($\u会话)。