douduidui1046 2012-12-18 12:31
浏览 186
已采纳

检查用户是管理员还是普通用户? PHP $ _session和MYSQL

I want to show different content if a user is admin(2), or noarmal user(1). Login works fine, but i don't know hot to check if a user have 'usertype' 1 or 2? I want to use PHP $_SESSION.

This is my login form:

<!-- Log in -->
<form method="post" action="authentication.php">
  <input type="text" name="userid" placeholder="E-mail" required/>
  <input type="password" name="password" placeholder="Password" required/><br>
  <button type="submit" name="submit" class="btn">Log in</button>
</form>

This is my authentication.php:

session_start();
if(isset($_POST['userid']) && isset($_POST['password'])){
$userid     = $_POST['userid'];
$password   = $_POST['password'];

$dbc        = mysqli_connect('localhost', 'root', '', 'news');
if(!$dbc){
echo "Cannot connect to database";
exit;
}

$query      = "SELECT * FROM users_tbl WHERE email = '$userid' AND password = sha1('$password')";
$result     = $dbc->query($query);
if($result->num_rows)
{
$_SESSION['valid_user'] = $userid;
}
$dbc->close();
}
if(isset($_SESSION['valid_user'])){
header("location:prefs.php");
}else{
header("location:login.php");
echo 'Error';
}

This is what i have tried:

    <?php 

    mysql_connect('localhost', 'root', '');
    mysql_select_db('news');

    $sql = "SELECT users_tbl.usertype FROM users_tbl";
    $result = mysql_query($sql); 
    $user = mysql_fetch_array($result);
    $_SESSION['user'] = $user['user']; 
    $_SESSION['usertype'] = $user['usertype'];        

    if($_SESSION['user']['usertype'] == 2)
    {?>
    <h1>Only admin stuff</h1>

    <$? }//Endif user is admin(2) ?>

Maybe instead of doing the query for everytime to check if a user is admin, i could save a $_SESSION['usertype'], and then use this to check if a user is 2, for admin, maybe when a user is loggin in? But i do not know how to do this. I'm quite new at this.

  • 写回答

4条回答 默认 最新

  • 普通网友 2012-12-18 12:34
    关注

    try this

       if($_SESSION['usertype'] == 2)
    {
       //do stuff here 
    }
     if ($_SESSION['usertype']) == 1)
    
     {
     //do stuff here 
     }
    

    edit : if you dont want write so many stuff and you want just include admin stuff inside this one of users just do this

      if ($_SESSION['usertype']) == 1 or $_SESSION['usertype']) == 2)
    
       {
                  //do stuff here for them both admin and users
             if($_SESSION['usertype'] == 2 ) 
                                       {
                                      //do extra stuff here for only admin 
                                        }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?