我正在尝试创建一个登录页面,该页面将根据用户的登录凭据将用户发送到不同的 index.php 页面。例如,如果具有“IT Technician”角色的用户登录,他们将被发送到“index.php”,如果具有“Student”角色的用户登录,他们将被发送到“student/index” .php”页面。
我看不出我的代码有什么问题,但它不起作用......每次按下登录按钮时,我都会收到“错误的登录凭据”消息。
我的用户登录页面代码在这里:
<?php
session_start();
if (isset($_SESSION["manager"])) {
header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
exit();
}
?>
<?php
if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["role"])) {
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);
$role = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["role"]);
include "adminscripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
$existCount = mysql_num_rows($sql);
if (($existCount == 1) && ($role == 'IT Technician')) {
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["manager"] = $manager;
$_SESSION["password"] = $password;
$_SESSION["role"] = $role;
header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
} else {
echo 'Your login details were incorrect. Please try again <a href="http://www.zuluirminger.com/SchoolAdmin/index.php">here</a>';
exit();
}
}
?>
<?php
if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["role"])) {
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);
$role = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["role"]);
include "adminscripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
$existCount = mysql_num_rows($sql);
if (($existCount == 1) && ($role == 'Student')) {
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["manager"] = $manager;
$_SESSION["password"] = $password;
$_SESSION["role"] = $role;
header("location: http://www.zuluirminger.com/SchoolAdmin/student/index.php");
} else {
echo 'Your login details were incorrect. Please try again <a href="http://www.zuluirminger.com/SchoolAdmin/index.php">here</a>';
exit();
}
}
?>
从中提取数据的形式如下所示:
<form id="LoginForm" name="LoginForm" method="post" action="http://www.zuluirminger.com/SchoolAdmin/user_login.php">
User Name:<br />
<input type="text" name="username" id="username" size="50" /><br />
<br />
Password:<br />
<input type="password" name="password" id="password" size="50" /><br />
<br />
Log in as:
<select name="role" id="role">
<option value="">...</option>
<option value="Head">Head</option>
<option value="Deputy Head">Deputy Head</option>
<option value="IT Technician">IT Technician</option>
<option value="Pastoral Care">Pastoral Care</option>
<option value="Bursar">Bursar</option>
<option value="Secretary">Secretary</option>
<option value="Housemaster">Housemaster</option>
<option value="Teacher">Teacher</option>
<option value="Tutor">Tutor</option>
<option value="Sanatorium Staff">Sanatorium Staff</option>
<option value="Kitchen Staff">Kitchen Staff</option>
<option value="Parent">Parent</option>
<option value="Student">Student</option>
</select><br />
<br />
<input type="submit" name = "button" id="button" value="Log In" onclick="javascript:return validateLoginForm();" />
</h3>
</form>
登录后(如果加载了正确的页面,我在脚本顶部的验证代码如下所示:
<?php
session_start();
if (!isset($_SESSION["manager"])) {
header("location: http://www.zuluirminger.com/SchoolAdmin/user_login.php");
exit();
}
$managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
$role = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["role"]);
include "adminscripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM Users WHERE username='$manager' AND password='$password' AND role='$role' LIMIT 1");
$existCount = mysql_num_rows($sql);
if ($existCount == 0) {
header("location: http://www.zuluirminger.com/SchoolAdmin/index.php");
exit();
}
?>
请注意,数据库表具有以下字段:id、username、password和role。
任何帮助将不胜感激!
非常感谢,祖鲁语