简体   繁体   English

通过单个登录页面进行多用户类型登录

[英]Multiple User Type Login Through Single Login Page Issue

I am working on php and mysql code on making access to different pages based on the role of the user, through one Login Page. 我正在研究php和mysql代码,以便通过一个登录页面根据用户的角色访问不同的页面。

Its working good for 'admin' page .. 它对“管理员”页面有效。

but not able to login with 'normal type' 但无法使用“普通类型”登录

Little Help is really appreciated, Thank You 非常感谢您的帮助,谢谢

Here is my Code 这是我的代码

<?php

session_start();

include 'dbcon.php';

 if($_SERVER["REQUEST_METHOD"] == "POST") {

  $username = $_POST['username'];
  $password = $_POST['password'];

 $query = "SELECT * FROM wp_users WHERE user_login = '$username' AND user_pass = '$password'";

  $result = mysqli_query($con,$query) ; 

    $row = mysqli_fetch_assoc($result);

      $count=mysqli_num_rows($result)  ;

         if ($count == 1) {   

            if($row['user_type'] == 'admin')
           {
             header('Location: user_registration.php');
              $_SESSION['ID'] = $row['ID'];
               $_SESSION['user_login'] = $row['user_login'];
                $_SESSION['password'] = $row['user_pass'];
           }

           elseif($row['user_type'] = 'normal')
           {
             header('Location: index.php');
           }

           else
           {
            echo "WRONG USERNAME OR PASSWORD";
           }
        }

   }
?>

Move your session code after if condition and then redirect. 如果满足条件,请移动会话代码,然后重定向。 Also is there any specific reason to store password in session. 也没有任何特定原因在会话中存储密码。 == missing ==缺少

Use proper filters for inputs. 为输入使用适当的过滤器。

           if ($count == 1) { 
               if(!empty($row['user_type'])) {  
               $_SESSION['ID'] = $row['ID'];
               $_SESSION['user_login'] = $row['user_login'];
               //$_SESSION['password'] = $row['user_pass'];
               }
            if($row['user_type'] == 'admin')
           {
             header('Location: user_registration.php');

           }

           elseif($row['user_type'] == 'normal')
           {
             header('Location: index.php');
           }

           else
           {
            echo "WRONG USERNAME OR PASSWORD";
           }
        }

The logic test for the normal user was using a single = sign which sets a value rather than tests for equality - it needs to be == normal用户的逻辑测试使用单个=符号设置值,而不是相等性测试-它必须为==

Also, I think the WRONG USERNAME OR PASSWORD wa at the wrong level - it needs to be the else to the record count 另外,我认为WRONG USERNAME OR PASSWORD的级别错误-它需要是记录数的else

<?php

    session_start();

    include 'dbcon.php';

    if($_SERVER["REQUEST_METHOD"] == "POST") {

        $username = $_POST['username'];
        $password = $_POST['password'];

        $query = "SELECT * FROM wp_users WHERE user_login = '$username' AND user_pass = '$password'";
        $result = mysqli_query($con,$query);
        $row = mysqli_fetch_assoc($result);
        $count=mysqli_num_rows($result);
        if ($count == 1) {
            if($row['user_type'] == 'admin') {
                header('Location: user_registration.php');
                $_SESSION['ID'] = $row['ID'];
                $_SESSION['user_login'] = $row['user_login'];
                $_SESSION['password'] = $row['user_pass'];
            /* require `==` here */
            } elseif( $row['user_type'] == 'normal' ) {
                header('Location: index.php');
            } else { 
                die('unknown/unhandled user level');
            }
        /* changed location of this by one level */
        } else {
            echo "WRONG USERNAME OR PASSWORD";
        }
    }
?>

This is function for login. 这是登录功能。

It presumes password come from user with sha512 encryption (see js libs like https://github.com/emn178/js-sha512 ) - it's good for non-encrypted connections. 它假定密码来自具有sha512加密的用户(请参阅js库,例如https://github.com/emn178/js-sha512)-对于非加密连接非常sha512

It uses salt , and have some protection from brute force , CSRF , XSS and SQL-injection . 它使用 ,并具有免受brute forceCSRFXSSSQL-injection

static public function db_login($email, $p)
{
    if ($stmt = Site::$db->prepare(
        "SELECT id, password, salt, name
         FROM user
         JOIN contact ON contact_id = id
         WHERE email = ?
         LIMIT 1")
    ) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($user_id, $db_password, $salt, $name);
        $stmt->fetch();

        // hash the password with the unique salt
        $p = hash('sha512', $p . $salt);
        if ($stmt->num_rows == 1) {
            // If the user exists we check if the account is locked
            // from too many login attempts
            if (self::checkBrute($user_id) == true) {
                // Account is locked
                $res['code'] = 0;
                $res['reason'] = 'trylimit';
                $res['message'] = 'You try too many times. Come back on 30 minutes';
                return $res;
            } else {
                // Check if the password in the database matches
                // the password the user submitted.
                if ($db_password == $p) {
                    // Password is correct!
                    // Get the user-agent string of the user.

                    // CSRF
                    $user_browser = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_SPECIAL_CHARS);

                    // XSS protection as we might print this value
                    $user_id = preg_replace("/[^0-9]+/", "", $user_id);

                    Login::sec_session_start();
                    $_SESSION['user_id'] = $user_id;
                    $_SESSION['email'] = htmlspecialchars($email);
                    $_SESSION['name'] = htmlspecialchars($name);
                    $_SESSION['token'] = md5(uniqid(rand(), TRUE));
                    $_SESSION['login_string'] = hash('sha512', $p . $user_browser);
                    session_write_close();

                    // Login successful
                    $res['isLogined'] = 1;
                    $res['code'] = 1;
                    $res['name'] = $name;
                    $res['id'] = $user_id;
                    return $res;
                } else {
                    // Password is not correct
                    // We record this attempt in the database
                    $now = time();
                    Site::$db->query("INSERT INTO login_attempts(user_id, time) VALUES ('$user_id', '$now')");
                    $res['code'] = 0;
                    $res['reason'] = 'pass';
                    $res['message'] = 'Wrong password';
                    return $res;
                }
            }
        } else {
            // No user exists.
            $res['code'] = 0;
            $res['reason'] = 'user';
            $res['message'] = 'We have no such email';
            return $res;
        }
    }
    $res['code'] = 0;
    $res['reason'] = 'SQL-error';
    return $res;
}

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

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