dsd30433 2013-11-16 17:15
浏览 24
已采纳

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

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:

+----------------------------------+
|  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();
  • 写回答

1条回答 默认 最新

  • duanjing7651 2013-11-16 17:36
    关注

    Your query need to be modified. Add a condition to retrieve access level of those that are matching the current $_SESSION['sessionloginid']. 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();
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab