SESSION两个表用户和角色

时间:2016-06-27 11:32:26

标签: php

我有两张桌子:

Users : 

 - id_user : int;
 - lastname : varchar(255):
 - firstname : varchar(255);
 - login : varchar(255);
 - password : varchar(255);
 - roles_id : int (foreign key);

Roles : 
 - id_roles : int;
 - name : varchar(255);

我有一些数据:

Users : 
    1, lastname1, firstname1, login1, password1, 1
    2, lastname2, firstname2, login2, password2, 3
    3, lastname3, firstname3, login3, password3, 2

Roles : 
1, admin
2, moderator
3, user

我有一个菜单:

session_start();

<li><a><i class="fa fa-cog"></i> configuration<span class="fa fa-chevron-down"></span></a>
                  <ul class="nav child_menu" style="display: none">
                     <?php if($_SESSION['Auth']['roles_id'] == 1){
                                ?>

                        <li><a href="#">add user</a></li>
                        <li><a href="#">List Users</a></li>
                                <?php  


                            }elseif($_SESSION['Auth']['roles_id'] == 2){
                                ?>

                        <li><a href="#">add Product</a></li>
                        <li><a href="#">List Product</a></li>

          <?php }
                            elseif($_SESSION['Auth']['roles_id'] == 3){
                                ?>
                        <li><a href="#">add input</a></li>       
                        <li><a href="#">List inputs</a></li>

<?php } ?>

     </ul>

</li>

我想在条件中代替roles_id,我可以把角色的名称。例如:

<?php if($_SESSION['Auth']['name'] == 'admin'){
                                ?>
And
<?php if($_SESSION['Auth']['name'] == 'moderator'){
                                ?>
and
<?php if($_SESSION['Auth']['name'] == 'user'){
                                ?>

并查询保存值名称:

 extract($_POST);
     $password = sha1($password);
     $sql = "SELECT u.*, r.* FROM users as u INNER JOIN roles AS r ON r.id = u.roles_id WHERE login = '$login' AND password = '$password'";
     $req = $db->query($sql) 
    if($req ->rowCount()> 0){
        $data = $req->fetch(PDO::FETCH_ASSOC)) ;
        $_SESSION['Auth']= array(
            'login' => $login,
            'password' => $password,
            'roles_id' => $data['roles_id'],
            'lastname ' => $data['lastname'],
            'firstname' => $data['firstname'],
            'name' => $data['name'],

        );



}

但是当我做会话名称时:

$_SESSION['Auth']['name']

他给了我一个警告:Undefined index name.

  • 如何在会话中使用角色名称。

0 个答案:

没有答案