duanaiguang1960 2016-05-11 13:24
浏览 57
已采纳

从数据库中的表中获取一个特定字段并在会话中使用它

I'm currently building a website that has a log in form. Depending on which role this user has a navigation bar has to be displayed accordingly.

For Example : Someone who hasn't yet logged in will have a "login" link. If someone has logged in and is not an admin a different nav bar is displayed.

All log in details are stored in a db and this has to be done using sessions since it will send the user to a different page.

Any ideas ?

Code till now :

$query = "SELECT count(*) FROM tbl_users
            WHERE username = '$username' AND password = '$pass'";

$result = mysqli_query($connection, $query)
        or die("Error in query: ". mysqli_error($connection));

$row = mysqli_fetch_row($result);
$counter = $row[0];

if($counter > 0)
{
    session_start();
    $_SESSION['user']= $username;
    $_SESSION['role'] = $row['role'];
  • 写回答

1条回答 默认 最新

  • duanlu1793 2016-05-11 14:54
    关注

    a somewhat vague question but, check your role.

    if($_SESSION['role']=='admin')
        echo 'I am admin';
    elseif($_SESSION['role']=='customer')
        echo 'I am customer';
    else
        echo 'No valid role / not logged in';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢