我有一个重定向到管理员帐户的登录脚本,我想回显在登录函数中创建的一些会话变量,但我一直收到一个"未定义变量"通知。
这是我的登录脚本
$connect = mysqli_connect('localhost', 'root', '', 'cms1');
function login() {
global $connect;
$email = mysqli_real_escape_string($connect, $_POST['email']);
$password = mysqli_real_escape_string($connect, $_POST['password']);
$result = mysqli_query($connect, "SELECT * FROM users WHERE email='$email'");
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
if (password_verify($password, $row['password'])) {
session_start();
$_SESSION["id"] = $row['user_id'];
$_SESSION["name"] = $row['name'];
$_SESSION["email"] = $row['email'];
$_SESSION["gender"] = $row['gender'];
$_SESSION["role"] = $row['role'];
header('Location: admin/index.php#/dashboard');
} else {
header('Location: index.php#/posts/WrongPassword');
}
}
这是我的管理目录中的index.php文件的一个片段
<div class="pcard">
<img src="../images/T1.jpg" width="100%" alt="...">
<div class="body">
<h4><?php echo $_SESSION["name"]; ?></h4>
<h5>Admin</h5>
</div>
</div>
看起来您可能还没有开始会话。你需要这样开始:
session_start();
如果你没有在每个页面上启动会话,你需要在上使用$_SESSION
变量,你会得到这样的错误:
E_NOTICE : type 8 -- Undefined variable: _SESSION -- at line 3
如果您不确定如何使用PHP会话的文档,那么可能值得一看。