du5591 2017-02-25 06:37
浏览 885
已采纳

如何在PHP中为管理员和用户分别登录?

I'm a student and I want to explore more on PHP. I try using functions but I have issues that I dont understand. I want a separated login for the admin and user but always takes me to the first if statement.

            <?php
            session_start();

            $connect = mysqli_connect('localhost','root','');

                if (!$connect){

                        die ("Unable to connect ".mysqli_connect_error($connect));
                        exit();
                } 
                if (!mysqli_select_db($connect,'gram')) {

                    die ("Unable to select ".mysqli_error($connect));
                    exit();
                }


            $email = $_POST['email'];
            $password = $_POST['password'];
            $adminemail = "manager@gramcab.com";
            $adminpass = "gram123";

            function loginUser(){
                global $connect, $email, $password;
                $statement = mysqli_prepare($connect, "SELECT * FROM tbl_users WHERE email=? AND password=? LIMIT 1"); 
                    mysqli_stmt_bind_param($statement, "ss", $email, $password);
                    mysqli_stmt_execute($statement);
                    mysqli_stmt_store_result($statement);
                    $count = mysqli_stmt_num_rows($statement);
                    mysqli_stmt_close($statement); 
                    if ($count < 1){
                        return true; 
                    }else {
                        return false; 
                    }   
            }

            function loginAdmin(){

                global $connect, $adminemail, $adminpass;
                $statement = mysqli_prepare($connect, "SELECT * FROM tbl_gramusers WHERE email=? AND password=? LIMIT 1"); 
                    mysqli_stmt_bind_param($statement, "ss", $adminemail, $adminpass);
                    mysqli_stmt_execute($statement);
                    mysqli_stmt_store_result($statement);
                    $count = mysqli_stmt_num_rows($statement);
                    mysqli_stmt_close($statement); 
                    if ($count < 1){
                        return true; 
                    }else {
                        return false; 
                    }
            }

            if(loginUser()){
                header('Location:../index.html');
            }   
            if (loginAdmin()){
                header('Location:../admin.html');
            }



            ?>
  • 写回答

1条回答 默认 最新

  • douxuanling6523 2017-02-25 06:43
    关注

    In both function change

    $count < 1
    

    to

    $count == 1
    

    If in DB it matches a record and have one row then it should return true else false.

    In current scenario if no rows found it returns 0 and you condition satisfies it $count < 1 so it will always return true and first IF will execute only.

    Also, you can write the code is much better way like

    function login($tableName){
       global $connect, $email, $password;
       $query = "SELECT * FROM ". $tableName ." WHERE email=? AND password=? LIMIT 1"
       $statement = mysqli_prepare($connect, $query); 
       mysqli_stmt_bind_param($statement, "ss", $email, $password);
       mysqli_stmt_execute($statement);
       mysqli_stmt_store_result($statement);
       $count = mysqli_stmt_num_rows($statement);
       mysqli_stmt_close($statement); 
       return ($count == 1);
     }
    
    
    // Calling function to check login for admin and user
    
       if(login('tbl_users')){
          header('Location:../index.html');
       } else if (login('tbl_gramusers')){
          header('Location:../admin.html');
       } else {
         echo 'Wrong Username or password!';
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀