我正在为网站构建CMS。问题是登录后会出现一个空白页面,直到我点击刷新为止。然后它加载到正确的菜单页面,其他一切正常工作,除了这个小细节。解决这个问题的任何提示?谢谢,我的代码如下:
<?php
session_start();
include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) {
//display index
?>
<html>
<head>
<meta charset="UTF-8">
<title>AdminENG</title>
<link rel ="stylesheet" href="../assets/style.css"/>
</head>
<body>
<div class="container">
CMS - ENG
<ol>
<li><a href ="add.php">Add Article</a></li>
<li><a href ="delete.php">Delete Article</a></li>
<li><a href ="logout.php">Logout</a></li>
</ol>
</div>
</body>
</html>
<?php
}
else {
//display login
if(isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = md5($_POST['password']);
if (empty($username) || empty($password)) {
$error = "All fields are required!";
}
else {
$query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");
$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->execute();
$num = $query->rowCount();
if($num == 1) {
//user entered the correct details
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit();
}
else {
//user entered false details
$error = "Incorrect details!";
}
}
}
?>
<html>
<head>
<title>AdminENG</title>
<meta charset="UTF-8">
<link rel ="stylesheet" href="../assets/style.css"/>
</head>
<body>
<div class="container">
CMS
<br><br>
<?php
if (isset($error)) { ?>
<small style="color:#aa0000"><?php echo $error; ?></small>
<?php } ?>
<br><br>
<form action="index.php" method="post">
<input type ="text" name="username" placeholder="Username"/>
<input type="password" name="password" placeholder="Password"/>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
<?php
}
?>
答案 0 :(得分:4)
您的header()
重定向可能无效。检查错误日志以查看问题所在。 <{1}}重定向之前必须绝对没有字符发送到浏览器,否则它将失败。
我的猜测是脚本中header()
之前的那些空格(如果它们不是复制/粘贴错误)可能会干扰<?
重定向。
无论如何,检查一下head()
,看看你有什么。
答案 1 :(得分:0)
在向浏览器执行html后,您无法使用Header
。
请尝试将其替换为header('Location: index.php');
有了这个:
<script>window.location="index.php";</script>