如果管理员已经登录,我不想向管理员显示登录页面。如果我想在管理员转到管理员登录页面时将他们重定向到主页,我该怎么办?供您参考,我在管理员登录后已将header.php
包含在每个管理员页面中。非常感谢您的帮助!谢谢!
下面显示了我header.php
编码的一部分:
<?php
session_start();
if(!isset($_SESSION['name']) or (!isset($_SESSION['password']))
or $_SESSION['name'] == '' or $_SESSION['password'] == '' )
{
header('Location: index.php');
}
?>
下面显示了我的管理员登录页面编码:
<form method="post" action="login_now.php" name="form1" id="form1" onSubmit="MM_validateForm('Name','','R','Password','','R','Security_code','','R');return document.MM_returnValue">
<table style="width: 100%;">
<tbody>
<tr>
<td style="text-align: center;" rowspan="4"><img src="../admin/images/login.png" alt="Please enter your login details."></td>
</tr>
<tr>
<td>
Username<span class="required">*</span>
<br>
<input type="text" class="form-control" style="width: 150px; height: 20px" name="Name" id="Name" class="inputstyle"/>
<br>
<br>
Password<span class="required">*</span>
<br>
<input type="password" class="form-control" style="width: 150px; height: 20px" name="Password" id="Password" class="inputstyle"/>
<br>
</td>
</tr>
<tr>
<td> <br> </td>
</tr>
<tr>
<td align="right" width="240px">
<input type="submit" value="Login" class="buttons" />
下面显示了我的login_now.php
编码:
<?php session_start();
ob_start();
include("../DBScripts/DB.php");
$adminName = $_POST['Name'];
$adminPassword = $_POST['Password'];
$Security = $_POST['Security_code'];
$hidden = $_POST['Security_required'];
$conn = dbConnect();
if (!$conn)
die("Couldn't connect to MySQL");
$query = "select * from user where usernm='$adminName' and userpw='$adminPassword'";
$result = mysql_query($query, $conn);
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0 && $row['userty']== user && $Security == $hidden)
{
$_SESSION['name'] = $adminName;
$_SESSION['password'] = $adminPassword;
header ('Location: ../Users/home_user.php');
}
else if(mysql_num_rows($result) > 0 && $row['userty']== admin && $Security == $hidden)
{
$_SESSION['name'] = $adminName;
$_SESSION['password'] = $adminPassword;
header('Location: adminindex.php');
}
else
{
header('Location: index.php');
}
?>
我建议的第一件事是你永远不想在会话中存储密码信息。密码应仅用于对登录操作进行身份验证,甚至此比较也应进行哈希处理。
除此之外,你几乎拥有你所拥有的东西。如果用户已登录并且您已将其用户名存储在 $_SESSION
中,您可以只测试 isset($_SESSION['name'])
,然后根据需要重定向。只需确保实现取消设置$_SESSION['name']
的注销功能即可。
如果我明白你在问什么,你只需要在你的登录页面中插入它,并从你的其余页面中删除标题.php:
if(isset($_SESSION['name'])){
header("Location: index.php");
die();
}
这将使在已登录的情况下无法访问登录页面。如果$_SESSION['name']
已初始化且不为 null,则用户已登录,否则不会。
然后你需要提供一个注销页面,最简单的方法是在一个名为logout的地方创建一个链接,该链接链接到注销.php其中可能包含:
unset($_SESSION['name']);
header("Location: index.php");
创建特定于管理员的新会话。
登录时,设置管理员登录时的新$_SESSION['admin'] = true
(与其他会话一起设置)
然后在登录页面上,
if (isset($_SESSION['admin']) && $_SESSION['admin'] === true) # || is the same as the or operator
header('Location: yournonloginpage.php');
像这样检查是否已经创建并设置了会话,然后将用户重定向回adminindex
页面,您必须在登录页面中检查这一点:
session_start();
if( isset($_SESSION['name']) && $_SESSION['name'] != "" )
{
header('Location: adminindex.php');
die;
}