[英]Restrict PHP page from specific user
假設我有一個名為users的數據庫,它有3個用戶:
uid | a_id | username | password
1 | 1 | William | 123
2 | 2 | Joe |321
a_id表示“訪問ID”,表示1是admin,2是guest。
我希望a_id為2的用戶無法看到或單擊examplepage1.php
這是我用於登錄的代碼:
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=login', "root", "");
} catch (PDOException $e) {
echo $e->getMessage();
}
$uid = $_POST['username1'];
$pwd = $_POST['password1'];
$sql = "SELECT * FROM `users` WHERE `username` = :username1 AND `password` = :password1";
$statement = $db->prepare($sql);
$userData = [
'username1'=>$uid,
'password1'=>$pwd,
];
$statement->execute($userData);
if($statement->rowCount() > 0){
session_start();
$_SESSION['admin']= $uid;
$_SESSION['logged'] = true;
header('Location: indextemplate.php');
exit();
}
elseif ($uid!=$idvariable&$pwd!=$idvarible){
header('Location: loginform.php?error=empty2');
exit();
}
?>
在頁面的每個開始處:
<?php
session_start();
if(!isset($_SESSION['admin'])){
header('location:index1.php');
}
else
{
*my website here*
}
?>
使用用戶名和密碼登錄時,存儲您的a_id
。 並檢查
<?php
session_start();
if(isset($_SESSION['a_id']) && $_SESSION['a_id'] == 1){
header('location:index1.php');
}
else
{
*my website here*
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.