我对PHP很熟悉,想要建立一个登录/注册系统,但我遇到了我的代码问题。我试图测试用户是否已经登录,因此我可以根据需要重定向它们,但每当我强制登录并进入注册页面时,它都不会根据需要重定向我。请有人帮助我。这是我的代码。
Register.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>
的index.php
<?php
session_start();
$_SESSION['user_id'] = 1;
?>
答案 0 :(得分:3)
首先,register.php
在顶部也需要这个:
session_start();
基本上,任何页面都需要写入或读取会话。
其次,这不是重定向:
header('index.php');
这是:
header('Location: index.php');
浏览器可能只是忽略了一个不完整的标题,因为没有键告诉浏览器该如何处理该值。