我在页面上有循环重定向问题。我不知道怎么逃避它。我有一个标题页面,包含所有数据库连接和所有其他页面的会话检查。我需要将它包含在登录页面中,但我不希望菜单显示在其上,我只需要数据库连接的标题。 但我无法做到,因为我有会话检查,我继续获取浏览器消息'循环重定向'。 有没有办法解决它,仍然有登录页面上的标题? 我的代码: admin_header
<?php
session_start();
define('ADMIN',$_SESSION['username']);
if(!$_SESSION['username'])
{
header("location:admin_login.php");
}
?>
<html>
<head>
<title>Project</title>
<link type="text/css" rel="stylesheet" href="styles.css"/>
</head>
<body id="home">
<div class="top" id="header">
<ul>
<li><a class="menu" href="index.php">Home</a></li>
<li><a class="menu" href="search.php">Add an Artist</a></li>
<li><a class="menu" href="contact.php">List the Artists</a></li>
<li><a class="menu" href="contact.php">Add a movie</a></li>
<li><a class="menu" href="contact.php">List the movies</a></li>
<li><a class="menu" href="contact.php">Add an admin</a></li>
<
</ul>
</div>
<?php
$server = 'localhost';
$user = 'root';
$pass = 'blablabla';
$mydb = 'projectdb';
$movies = 'movies';
$artists='artists';
$roll='roll';
$users='Users';
$connect = mysqli_connect($server, $user, $pass);
mysqli_select_db($connect,$mydb)or die("cannot select DB");
session_start();
define('ADMIN',$_SESSION['username']);
if(!$_SESSION['username'])
{
header("location:admin_login.php");
}
?>
</body></html>
admin_login
<?php
include ('admin_header.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
$login_form = <<<EOD
<form name="login" id="login" method="POST" action="">
<p><label for="username">Please Enter Username: </label><input type="text" size="40" name="username" id="username" value="" /></p>
<p><label for="password">Please Enter Password: </label><input type="password" size="40" name="password" id="password" value="" /></p>
<p><input type="submit" name="submit" id="submit" value="Submit"/> <input type="reset" name="reset" id="reset" value="reset"/></p>
</form>
EOD;
if (isset($_GET['msg']))
{
$msg=$_GET['msg'];
if($msg!='')
{
echo '<p>'.$msg.'</p>';
}
}
echo "<h1>Please enter your Login Information</h1>";
echo $login_form;
ob_start();
$server = 'localhost';
$pass = 'blablabla';
$mydb = 'projectdb';
$users='Users';
$connect = mysqli_connect($server, $user, $pass);
mysqli_select_db($connect,$mydb)or die("cannot select DB");
if (!isset($_POST['username'])||!isset($_POST['password']))
{
echo "Please, enter your login information";
}
else
{
$username = $_POST['username'];
$password = $_POST['password'];
if (!$connect)
{
echo ("Cannot connect to $server using $user");
}
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connect,$username);
$password = mysqli_real_escape_string($connect,$password);
$sql="SELECT * FROM $users WHERE username='".$username."' and password='".$password."'";
$result=mysqli_query($connect,$sql);
$count=mysqli_num_rows($result);
if($count==1)
{
session_start();
header("location:admin_index.php");
$_SESSION["username"]=$username;
$_SESSION["password"]=$username;
}
else
{
$msg = "Wrong Username or Password. Please retry";
echo $msg;
}
}
ob_end_flush();
?>
</body>
</html>
非常感谢您的帮助!
答案 0 :(得分:0)
代码中有两次,一次在顶部,一次在底部。我建议将它放在脚本的顶部,并在检查完会话后进行定义。另外,我加了一个额外的支票:
session_start();
if(!$_SESSION['username'] && $page <> 'logon')
{
header("location:admin_login.php");
}
define('ADMIN',$_SESSION['username']);
然后。在您的管理员登录中:
<?php
$page = logon;
include ('admin_header.php');
?>
答案 1 :(得分:0)
在admin_header中更改此代码:
define('ADMIN',$_SESSION['username']);
if(!$_SESSION['username'])
{
header("location:admin_login.php");
}
用这个:
if(!$_SESSION['username'] && $page != 'login')
{
header("location:admin_login.php");
} elseif(isset($_SESSION['username'])) {
define('ADMIN',$_SESSION['username']);
}
并在admin_login中添加以下行:
$page = 'login';
在包含标题之前。
修改强>
使用elseif条件尝试删除未定义的错误