我们说我有一个名为用户的数据库,它有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*
}
?>
答案 0 :(得分:0)
使用用户名和密码登录时存储a_id
。并检查
<?php
session_start();
if(isset($_SESSION['a_id']) && $_SESSION['a_id'] == 1){
header('location:index1.php');
}
else
{
*my website here*
}
?>