【问题标题】:Check multiple variables to verify session access to page检查多个变量以验证对页面的会话访问
【发布时间】:2016-03-04 11:06:18
【问题描述】:

对于我的大部分网站,我可以很高兴地验证用户 ID 是否存在,以便向注册用户显示受限内容。但我想开发一个管理界面以便于管理,并且仅在满足两个(或更多)条件时才允许访问特定的一组页面。

我想到的两个条件是用户 ID(每个注册用户都有一个),但是我想验证该用户的角色并可以选择从我的 user_role 列中提取一个值桌子。目前有三种可能性;管理员、编辑和读者。

我不太清楚如何让查询触发检查,我现有的会话代码是;

// Here we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, redirect them to the login page. 
        header("Location: login.php"); 
        die("Redirecting to login.php"); 
    } 

如何添加额外的$_SESSION 变量来检查用户的角色?

【问题讨论】:

  • 我不太明白你的问题。当您将某人登录到您的管理界面时,您可以在会话中添加 user_role。或者只是将 user_id 放入会话中并查询您的数据库以检查 user_role。这是你的问题吗?
  • 是 - 当具有管理员角色的用户在 domain.com/admin/ 登录时,会检查表并登录。如果他们具有编辑器或阅读器等角色,他们将被重定向到域.com/login/ 因为他们没有管理员权限。
  • 试试这个:if(empty($_SESSION['user'])) { // If they are not, redirect them to the login page. header("Location: login.php"); die("Redirecting to login.php"); } else { /*query to check the user role*/ }
  • @RaphaëlGonçalves 这是我遇到最大困难的问题。我熟悉 if 语句。
  • 你需要共享你的表结构

标签: php sql session authentication


【解决方案1】:

也许可以尝试这样的事情(假设在登录和注册时您设置了一个包含用户 ID 或其他标识符的 $_SESSION 变量):

if(empty($_SESSION['user'])) 
    { 
        // If they are not, redirect them to the login page. 
        header("Location: login.php"); 
        die("Redirecting to login.php"); 
} else 
    {
        include_once("databaseConnection.php");
        $checkAdmin = "SELECT user_role FROM users WHERE id = '".$_SESSION['user_id']."';";
        $result = mysqli_query($conn, $checkAdmin);
        $user_role = mysqli_fetch_assoc($result);
        if ($user_role == "reader") 
            {
                // User is only a reader, redirecting them to the home page
                header("Location: /");  
                die("You're only a reader and you're not allowed here, redirecting you to the home page!");
        } elseif ($user_role == "editor")
            {
                // User is only an editor, redirecting them to the home page
                header("Location: /");
                die("You're only an editor and you're not allowed here, redirecting you to the home page!");        
        } elseif ($user_role == "editor")
            {
                // User is an admin, allowing them through
        } else
            {
            //Error identifying user's role
            header("Location: /");
            die("Some kind of error occured, show this to the site owner: (User role identification error)");
        }
    }

【讨论】:

【解决方案2】:

我推荐的方法还会考虑到此类特权用户的实际数量。如果您有很多这样的用户,请考虑在服务器上增加额外的负载,在每个页面加载时检查每个用户的数据库,在这种情况下,我会使用会话存储变量来授予对某些页面的访问权限和功能。会话实际上存储在服务器上,因此用户不能像编辑 cookie 一样对其进行编辑,因此这应该提供足够的安全性。

该解决方案还应考虑您希望它的可扩展性和灵活性。如果您只发现一些受限权限,您可以很好地启用或禁用整数中的位并将其存储在会话变量中。然后只需用一个简单的面具检查右边。另一方面,如果您发现不只是几个权限,则可以使用一系列用户权限。例如:“ab”应该赋予用户添加新帖子(a)和编辑其他用户帖子(b)的权限。这种方法应该提供更多的可扩展性。我在一个实际的 php 实现中使用了这种方法,用于类似的后端管理案例。

如果您想要在不需要用户再次登录的即时效果方面更具响应性的东西,我会选择更接近 MQTT 的东西,因为它非常可扩展且易于管理。为此,您可以使用开源 Paho 客户端并实例化每个登录,以便您可以在它们之间异步发送数据。这个功能也可以使用一些 JavaScript 库来实现,但我有点过时了,因为几年前我没有看过这些。

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2012-01-17
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多