验证用户是否为admin

yvgpqqbh  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(270)

这个问题在这里已经有答案了

引用-这个错误在php中是什么意思(36个答案)
如何跨页面使用存储和会话变量(8个答案)
如何显示mysqli查询的错误[重复](2个答案)
使用密码\u散列和密码\u验证[重复](1个答案)
两年前关门了。
我正在尝试设计一个基本的网站供内部使用。。。
在数据库中,我有一个用户表,该表由“id,username,password,email,admin”组成。admin的值可以是0或1
我想验证登录的用户是否是管理员,然后根据结果选择要向他们显示的内容。
我已经尝试了很多不同的建议,从多个网站包括这里。。。我最近尝试使用的代码是

if (!isset($_SESSION['id']) || !in_array($_SESSION['id'], array('1'))) {

我将在下面发布完整的代码,并解释我希望得到的结果

<?php
session_start();
// check to see if the user is logged in
if ($_SESSION['loggedin']){
// user is logged in
  ?><div id="container"><?php
    echo 'Welcome ' . $_SESSION['name'] . '!' . '|' . '<a href="logout.php">logout?</a></div>';

?>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<img src="images/logo.gif" class="bg">
<?php if (!isset($_SESSION['admin']) || !in_array($_SESSION['admin'], array('1'))) {
?>
<div id="container">
<div id="box">
  <h2>: Add User :</h2>
<p>
  <form action="action_adduser.php" method="post">
Username:  <input type="username" id="newuser" name="username"> <br />
Password:  <input type="password" id="newpassword" name="password"><br />
Email:  <input type="email" id="email" name="email"><br />
  <input type="submit" value="Add!">
  </form>
</p>
</div>
</div>

<?php
}
}elseif ($_SESSION['loggedin']) {
  # User is Logged in without any special permissions.
}else{
    // user is not logged in, send the user to the login page
    header('Location: login.php');
}
?>

如果我的帐户的admin列设置为“1”,我希望(登录后)看到一个表单,向数据库中添加一个新用户。否则,我将看不到adduser表单或无法添加新用户。
我可以确认在回显$\u session['admin']时,它会回显该字段设置为i、e 1或0的任何内容,所以我猜验证部分的代码是错误的?
我想在一开始就检查$\u session['logged\u in']是否是通过&&$\u session['admin']之类的操作设置的,但我不知道如何实际验证它的1或0是否与代码一致,然后只检查它的设置是否正确?
希望这是有意义的,并感谢任何帮助我的问题:)
验证.php

<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'withheld';
$DB_NAME = 'withheld';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
    // If there is an error with the connection, stop the script and display the error.
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    die ('Username and/or password does not exist!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT id, password, admin FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    $stmt->store_result();
    // Store the result so we can check if the account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password, $admin);
        $stmt->fetch();
        // Account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['admin'] = $admin;
            $_SESSION['id'] = $id;
            echo 'Welcome ' . $_SESSION['name'] . '!';
            header('Location: index.php');
        } else {
            echo 'Incorrect username and/or password!';
        }
    } else {
        echo 'Incorrect username and/or password!';
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
?>
djp7away

djp7away1#

你把事情复杂化了,做个例子:
您有两个(甚至更多)选项:
继续使用会话管理,然后您只需要:
if($\u session['admin']==1){…//您不需要检查会话是否存在,因为您已经设置了它
使用$\u session['id']检查每个页面加载的admin值,我建议这样做,因为使用许多不同的会话不是一个好主意。

相关问题