最近,我开始创建自己的博客,所以首先我开始制作管理面板。 在该管理面板中,我首先在页面中添加了管理员登录。我使用了会话变量。但不知怎的,他们没有工作。
这是我的登录页面代码:
<?php
session_start();
if(isset($_POST['submit']))
{
$user = $_POST['username'];
$pass = $_POST['password'];
include('../includes/db_connect.php');
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(empty($user) || empty($pass))
{
$msg = 'Missing Information';
}
else
{
$pass = md5($pass);
$query = "SELECT * FROM `users` WHERE `username` = '$user' && `password` = '$pass'";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result) == 1)
{
while($row = mysqli_fetch_array($result))
{
$_SESSION['user_id'] = $row['id'];
header('Location: index.php');
}
}
else
{
$msg = 'Invalid username or password';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log In</title>
<link href="../css/login.css" type="text/css" rel="stylesheet" media="screen" />
</head>
<body>
<div id="container">
<form id="login" action="login.php" method="post">
<h1 id="ff-proof" class="ribbon">Admin Login </h1>
<fieldset id="inputs">
<input id="username" name="username" type="text" placeholder="Username" />
<input id="password" name="password" type="password" placeholder="Password" />
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" name="submit" value="Log In"/>
<span style="color: red;"><?php if(isset($msg)){echo $msg;} ?></span>
</fieldset>
</form>
</div>
</body>
</html>
如果我使用正确的凭据,它会起作用。 但在进入管理索引页面后,服务器不再记住会话变量。这是管理员索引页面代码:
<?php
session_start();
if(!isset($_SESSION['user_id']))
{
header('Location: login.php');
}
?>
after that I have my index page code
但由于服务器不记得会话变量,我被重定向回登录页面。请帮忙。我尝试了很多次,但我无法解决问题。