【问题标题】:Validate if user is admin [duplicate]验证用户是否是管理员 [重复]
【发布时间】:2018-12-30 20:36:00
【问题描述】:

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

在数据库中,我有一个用户表,该表由“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');
}
?>

如果我的帐户将管理列设置为“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!';
}
?>

【问题讨论】:

  • 添加login.php 的代码和(如果没有)您创建和填充会话的部分。
  • 感谢@rpm192,不幸的是,您提供的代码不起作用,但我感谢您的建议!
  • @Gabriel 我可以在必要时发布 login.php 的代码!我已确保 admin 变量已设置且可读,但我也会为您发布登录脚本! :)
  • 看起来不错。将if (!isset($_SESSION['admin']) || !in_array($_SESSION['admin'], array('1'))) 替换为if($_SESSION['admin']=='1') 就足够了。虽然“用户在没有任何特殊权限的情况下登录”部分不正确,但仔细观察,elseif 是第一个if
  • 你试过&lt;pre&gt;&lt;?php var_dump($_SESSION); ?&gt;&lt;/pre&gt;检查$_SESSION['admin']包含的值吗?

标签: php mysql validation session


【解决方案1】:

你把事情复杂化了,让它成为样本:

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

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

    if ($_SESSION['admin'] == 1) { ... // 你不需要检查会话是否存在,因为你设置了它

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

【讨论】:

  • 感谢您的回答!我有点困惑,因为我尝试了 if ($_SESSION['admin'] == 1) { ... // 代码,但这不起作用对于你的建议 2,我真的不明白如何实现.. $_SESSION['id'] 是用于 id 列而不是 admin 列吗?谢谢!
  • 好吧,忘记第二点,继续你的理解,在你的代码中替换: if (!isset($_SESSION['admin']) || !in_array($_SESSION['admin' ], array('1'))) with : if ($_SESSION['admin'] == 1), 那应该可以工作:)
  • 谢谢!事实证明它一直在工作!更改数据库中的管理员值后,我正在刷新页面,但我需要注销并重新登录才能看到更改,因为它只记录登录时在数据库中设置的管理员值并将其值保存为会话变量!
  • 我认为如果该值发生变化,是否有办法让它自动更新?我会将您的答案标记为解决方案,但请留出一些时间让您回复,因为我不确定他们是否会在选择答案后锁定回复能力?再次感谢你的帮助! :)
  • 是的,因为您在登录脚本运行时创建会话,您不能仅通过刷新页面来更改它,并且很乐意帮助并听到您的问题已解决 :) @BradAndrews
猜你喜欢
  • 2014-08-31
  • 1970-01-01
  • 2019-10-23
  • 2019-05-25
  • 1970-01-01
  • 2023-03-08
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多