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

如何在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!';
       }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题