你可以有一個表users
與模式一樣,
user_id | name | password | type
1 admin admin 1
2 memeber member 0
例如,你可以有1爲admin和0的成員。
所以,在形式上可以有一個代碼,
<form action="validate.php" method="post">
Login: <input type="text" name="name">
Password: <input type="password" name="password">
<input type="submit" name="login" value="Login">
</form>
在validate.php
,檢查他是否是一個管理員或學生,
<?php
$db = mysqli_connect("specify host here","specify db username","specify db password","specify db name");
if(isset($_POST['login']) // check whether user submits the form
{
$name_b = $_POST['name'];
$password_b = $_POST['password'];
$name = mysqli_real_escape_string($db,$name_b); // escaping special characters
$password = mysqli_real_escape_string($db,$password_b);
$query = "SELECT `type` FROM `users` WHERE `name` = '$name' AND `password` = `$password` LIMIT 1 ";
$result = mysqli_query($db, $query)
if($result)
{
$row = mysqli_fetch_assoc($result);
$user_type = $row['type']; // you get user type here whether he's admin or login
if ($user_type == 1) {
// this use is admin
// do stuff here or redirect to admin panel or wherever you want
}
elseif ($user_type == 0) {
// this user is member
// do stuff here or redirect wherever you want
}
else
{
// if there's some other value, simplyredirect to login page again
}
} // close of if($result)
else
{
echo "query failed"; // redirect to login page again
}
}
else
{
// redirect to login page again
}
?>
如果你打算用這種風格來做,我會建議一個名稱爲「userType」的單選按鈕。將一個值設置爲'admin',將另一個值設置爲'student',然後檢查(在PHP部分中)$ _POST ['userType']'以查看它是管理員還是學生。 –
爲什麼不只是具有相同的登錄功能,然後相應地修改用戶對象/會話? – chris85
@史蒂夫感謝您的建議史蒂夫。 – Priyaranjan