验证用户是否为管理员

时间:2018-12-30 20:36:01

标签: php mysql validation session

我正在尝试设计一个供内部使用的基本网站...

在数据库中,我有一个用户表,该表由“ id,用户名,密码,电子邮件,管理员”组成 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”,我希望(一旦登录)看到一个向数据库添加新用户的表单。 否则,我希望看不到添加用户表单或能够添加新用户。

我可以确定在回显$ _SESSION ['admin']时,它确实回显了将该字段设置为i或1或0的任何内容,因此我猜测的验证部分中的代码是错误的吗?

我想从一开始就检查$ _SESSION ['logged_in']是否已设置,方法可能是执行&& $ _SESSION ['admin']之类的操作,但是我不知道如何实际验证它是否与代码相对应是1还是0,然后仅检查其是否已设置?

希望这很有意义,并感谢您对我的问题的帮助:)

Auth.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!';
}
?>

1 个答案:

答案 0 :(得分:0)

您正在使事情复杂化,请使其成为示例:

您有两个(甚至更多)选项:

  1. 继续使用会话管理员,那么您只需要:

    if($ _SESSION ['admin'] == 1){... //您无需检查会话是否存在,因为无论如何都可以对其进行设置

  2. 使用$ _SESSION ['id']检查每个页面加载时的管理员值,我建议这样做,因为使用许多不同的会话不是一个好主意。