dongzhiyi2006 2012-04-04 21:44
浏览 39
已采纳

为特定用户创建特定会话

I am trying to create a sessions and entry to pages based on a person's "role" in the database. I thought I nailed the logic and I created the code. It worked. Problem is, it works for everyone. I wanted some help to point out why my code is logically incorrect when to me it's on point. Any help will be greatly appreciated. Here is my code. It seems to let in anyone who's role is not 'Admin' and I can pin point why:

function auth($email, $pass){
        global $result;

        while ($row = mysql_fetch_array($result)) {
            if($email == $row["email"] && $pass == $row["password"])
            {
                return true;
            }
        }
        return false;
    }

    function test($email, $pass){
        $test1 = auth($email, $pass); <--above
        if ($test1) {
           get_role($email); <-- below (This is the one where I'm puzzled)
        $_SESSION['email'] = true;
            header('location:menupage.php');
            exit;      
        }
        else{
            echo "Please enter correct username and password or <a href='register.php'>Register</a>";}
    }

    function get_role($email){
    global $connection;
    $sql="SELECT * FROM exam WHERE email= '$email'";
    $result = mysql_query($sql, $connection);
    confirm_query($result);
    while ($row = mysql_fetch_array($result)) {
        if ($row["role"] == 'Admin'){
                $_SESSION['role'] = true;
            }
            else
            $_SESSION['role'] = false;
    }


    }

and this is the header for the administrative page that has the session

<?php
session_start();
if(!isset($_SESSION['role']))
{   $_SESSION['msg'] = 'you are not logggedd in';
    header('location: menupage.php');
    exit;
}
?>
  • 写回答

1条回答 默认 最新

  • dongyuli0964 2012-04-04 21:47
    关注

    $_SESSION['role'] = false is still a variable that is set. so !isset($_SESSION['role']) will not evaluate as you expect. Use something like:

    Admin page:

    if($_SESSION['role'] === false){ ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?