duancui19840401 2014-10-13 18:08
浏览 41
已采纳

显示某些级别PHP的内容

I'm creating a simple staff panel that consists of just about 10 pages and uses a single sql table. My sql table consists of user_id, user_name, user_email, password and rank

I'm trying to display specific content based on what the users rank is in the database although, I'm a very beginner at PHP so I'm not even sure if I'm pulling it from the database properly.

                $sql = "SELECT user_name, user_email, user_password_hash, user_id, rank
                    FROM users
                    WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';";
            $result_of_login_check = $this->db_connection->query($sql);

            // if this user exists
            if ($result_of_login_check->num_rows == 1) {

                // get result row (as an object)
                $result_row = $result_of_login_check->fetch_object();

                // using PHP 5.5's password_verify() function to check if the provided password fits
                // the hash of that user's password
                if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {

                    // write user data into PHP SESSION (a file on your server)
                    $_SESSION['user_name'] = $result_row->user_name;
                    $_SESSION['rank'] = $result_row->rank;
                    $_SESSION['user_email'] = $result_row->user_email;
                    $_SESSION['user_login_status'] = 1;

This is my login.php file.
This is the code I'm using to try and display something to a specific rank.

I've managed to do it so it will display something depending on the users name which leads me to believe I haven't called rank properly.

<?php
if (isset($_SESSION['rank']) && $_SESSION['rank'] == admin) {
echo '<a href="/admin/" class="btn btn-danger">Admin</a>';
} else {
echo " ";
}
?>
  • 写回答

1条回答 默认 最新

  • douchunjing6587 2014-10-13 23:57
    关注

    If the code you have posted is the exact same code you have on your files, then I believe the following line of code is your problem:

    if (isset($_SESSION['rank']) && $_SESSION['rank'] == admin) {
    

    Unless admin as been defined through the define function then I take it it is a string, therefore your code should be:

    if (isset($_SESSION['rank']) && $_SESSION['rank'] == 'admin') {
    

    Or:

    if (isset($_SESSION['rank']) && $_SESSION['rank'] == "admin") {
    

    Either single or double quotes would be fine. I don't see any other reason for the code to fail.

    I hope this helps.

    Follow up:

    You can use the next piece of code to use different ranks as per your comment:

    if (isset($_SESSION['rank'])) {
      if ($_SESSION['rank'] == 'admin') {
        // do admin
      } elseif ($_SESSION['rank'] == 'mod') {
        // do mod
      } elseif ($_SESSION['rank'] == 'member') {
        // do member
      }
    }
    

    You can also use the switch / case clause:

    if (isset($_SESSION['rank'])) {
      switch ($_SESSION['rank']) {
        case: 'admin':
          // do admin
          break; // do not forget the breaks!
    
        case: 'mod':
          // do mod
          break;
    
        case: 'member':
          // do member
          break;
      }
    }
    

    Hope this helps you even more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作