新手寫程式是這樣,都寫到這了發現這個問題
處理自己寫的php網頁
想依登入帳號的權限開啟某特定網頁
因為在帳號創建會選擇權限
下拉式選單:例 1 三種權限
2
3
此選單創建後寫入資料位於資料庫內表單的mission欄位
(帳號、密碼、mission都在同一資料表)
現在要如何在登入時確認帳號密碼(這部分已寫好)之後再比對mission欄位的值是1或是2還是3
然後導引至相對應的網頁
可能有點饒舌!! 有勞大大們指點
給你一個極度簡易的示意範例
front.php
<form action = "./login.php" method = "post">
<label for = "account">Account</label>
<input type = "text" id = "account" name = "maccount"><br>
<label for = "password">Password</label>
<input type = "password" id = "password" name = "mpassword"><br>
<button type = "submit">Login</button>
</form>
<style>
* {
margin: 5px;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
</style>
login.php
<?php
$users = array(
array("account" => "user1", "password" => "123", "mission" => "1"),
array("account" => "user2", "password" => "123", "mission" => "2"),
array("account" => "user3", "password" => "123", "mission" => "3"),
);
if (isset($_POST["maccount"]) && isset($_POST["mpassword"]) ) {
$account = $_POST["maccount"];
$password = $_POST["mpassword"];
$userData = null;
foreach($users as $key => $val) {
if ($val['account'] == $account) {
$userData = $users[$key];
}
}
if (!$userData) {
echo "login fail";
return;
}
if ($password == $userData['password']) {
switch ($userData['mission']) {
case '1': {
header( 'Location: 1.php' );
break;
}
case '2': {
header( 'Location: 2.php' );
break;
}
case '3': {
header( 'Location: 3.php' );
break;
}
default: {
break;
}
}
} else {
echo "login fail";
}
}
?>
共有三個 user
分別有不同權限
登入後會導向各身分的頁面
這只是示意
不同身分來導向不同頁面而已
至於那些
連資料庫
密碼不該以明文儲存
session 之類的
這裡不考慮