简体   繁体   English

sessionloginid上的这段代码有什么问题

[英]what's wrong with this code at sessionloginid

this is my first code I wrote in php I follow tutorials.. I want this code to select all fields in privilege table and check the id of user who logged in and his access level but the access level doesn't change with every user logged in 这是我在php中编写的我的第一篇代码,我遵循了教程。.我希望此代码选择特权表中的所有字段,并检查登录用户的ID和其访问级别,但访问级别不会随着每个登录用户的变化而改变在

I use relation between two tables they are: privilege table 我使用两个表之间的关系:特权表

+----------------------------------+
|  AccessLevel | logi_id  | pre_id |
|----------------------------------|
|      1       |    1     |   1    |
|      2       |    1     |   2    | 
|      4       |    2     |   4    |
+----------------------------------+

and this is login_pre table: 这是login_pre表:

+----------------------------------+
|  username| userpass | login_id   |
|----------------------------------|
|      a   |    123   |   1        |
|      a   |    123   |   1        | 
|      b   |   1234   |   2        |
+----------------------------------+

this is access page : 这是访问页面:

     <?php
        ob_start();
        session_start();
        include 'C:\xampp\htdocs\database\agtdatabase\agt_site\connection\connect.php';
        $query ="SELECT * FROM privilege " ;
        $result = mysqli_query($link,$query) or die('');

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                    {
            $access = $row['AccessLevel'];

                echo $access; //result 124 in database

                        }

            if(isset($_SESSION['sessionloginid']))// point to id of user logged in
            {           
            echo $_SESSION['sessionaccess']=$access;// "that is print  wrong result " access level doesn't change based on user logged in 

            echo $_SESSION['sessionloginid'];

                    }

            ob_end_flush();
        ?>

and this is login page: 这是登录页面:

        $username = $_POST['username'];
        $userpass = $_POST['userpass'];
        $loginid = $_POST['login_id'];


if($username && $userpass )
{
    $finduser = mysqli_query($link,"SELECT * FROM login_pre WHERE username = '".$username."' AND userpass = '".$userpass."'") or die("error");

    if(mysqli_num_rows($finduser) !=0)
        {

            while($row = mysqli_fetch_array($finduser))
                {$uname = $row['username'];
                $upass = $row['userpass'];
                $uloginid = $row['login_id'];
                }

        }

        if($username == $uname && $userpass == $upass  )
            {
                $_SESSION['sessionname'] =$uname;
            $_SESSION['sessionpass'] =$upass;
            $_SESSION['sessionloginid'] =$uloginid;

        echo $_SESSION['sessionloginid'];//result 124 of users

            }else header("location: login2.php");
ob_end_flush();

Your query need to be modified. 您的查询需要修改。 Add a condition to retrieve access level of those that are matching the current $_SESSION['sessionloginid'] . 添加条件以检索与当前$_SESSION['sessionloginid']匹配的access level Try this: 尝试这个:

<?php
      if(isset($_SESSION['sessionloginid']))// point to id of user logged in
        {  
        $query ="SELECT * FROM privilege where logi_id='".$_SESSION['sessionloginid']."'" ;
        $result = mysqli_query($link,$query) or die('');
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {
        $access = $row['AccessLevel'];

            echo $_SESSION['sessionaccess']=$access;
            echo $_SESSION['sessionloginid'];
                    }
                }

        ob_end_flush();
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM