doumie6223 2014-10-22 17:43
浏览 65
已采纳

检查登录用户的角色(PHP)

first, I have searched for a question that is the same with mine, unfortunately I can't understand the answers. It says use Auth, etc... bla bla bla. I only know basics so far.

So here is my question: how to check the user currently logged in and its role?

I thought I could do it so easily, actually I did, but the user of the site I'm building should only be one. lol. I have two columns named session and membership. Anyway, my code is written below (It is definitely wrong, I just realized it this 2AM in the morning. It would 100% work if the user of the side is again only one.

    if(empty($_SESSION['user']))
    {
        // If they are not, we redirect them to the login page.


        // Remember that this die statement is absolutely critical.  Without it,
        // people can view your members-only content without logging in.
        header("Location: http://localhost/se/");
    }
    //if(!empty($_SESSION['user']) )
    else
    {
        //This following codes are for checking the session in DB
        $query = "
            SELECT
                id,
                password,
                emailAddress,
                membership
            FROM memberlist
            WHERE
                session = :var_val
        ";
        // The parameter values
        $query_params = array(
           ':var_val' => 'True'
        );      

        try
        {
            // Execute the query against the database
            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);

        }
        catch(PDOException $ex)
        {
            // Note: On a production website, you should not output $ex->getMessage().
            // It may provide an attacker with helpful information about your code. 
            die("Failed to run query: " . $ex->getMessage());
        }
        $row = $stmt->fetch();



        if ( $row['membership'] == 'Officer'  || $row['membership'] == 'Member' )
        {
            header("Location: http://localhost/memberdir/index.php");
        }
}

If a user's membership == 1, then go to admin directory. else go to members directory.

Please help :(

  • 写回答

2条回答 默认 最新

  • dsf55s1233 2014-10-22 17:56
    关注

    To start a user session:

    session_start();
    

    To add parameters to that session:

    $_SESSION['parameter'] = $parameter;
    

    To get that parameter:

    $getParameter = $_SESSION['parameter'];
    

    So make sure you put session_start(); before any output to the page (before you print anything):

     if ( $row['membership'] == 'Officer'  || $row['membership'] == 'Member' )
            {
                session_start();
                $_SESSION['membership'] = $row['membership'];
                include('memberdir/index.php'); 
                //or  header("Location: http://localhost/memberdir/index.php");
            }
    

    So in your member file that you show a particular user (or only one user, doesn't make a difference), you check that the session parameter exists and what to do with it:

    if (isset($_SESSION['membership'])) {
        //membership parameter is set
        if($_SESSION['membership'] == 'Officer') {
            echo "Hey, your membership is Officer, here is your page";
        } else if ($_SESSION['membership'] == 'Member') {
            echo "Hey your membership is Member, here is your page";
        }
    }
    

    This should help you understand the basics and you can go from there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False