如何在login.php

时间:2018-03-29 10:54:05

标签: php mysqli login

我的帖子不是关于验证密码我知道怎么做,只是想首先进行广告用户角色重定向。 我更改了我的代码以验证和哈希密码只需要知道如何将成员角色重定向添加到login.php

我尝试了几个例子,但无法使其工作,因此添加了清晰的代码。

我有2种会员类型: 类型1.是管理员 类型2.是成员

需要将用户重定向到自己的网页 admin to admin / index.php 成员到member.php

感谢您的帮助

这是login.php中新的正确代码

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST['username'];
    $password = $_POST['password'];

    if(empty(trim($_POST["username"]))){
        $username_err = 'Please enter username.';
    } else{
        $username = trim($_POST["username"]);
    }
    // Check if password is empty
    if(empty(trim($_POST['password']))){
        $password_err = 'Please enter your password.';
    } else{
        $password = trim($_POST['password']);
    }
    // Validate credentials
    if(empty($username_err) && empty($password_err)){
        $sql = "SELECT username, password FROM users WHERE username = ?";

        if($stmt = $conn->prepare($sql)){
            $stmt->bind_param("s", $param_username);
            $param_username = $username;
            if($stmt->execute()){
                $stmt->store_result();
                if($stmt->num_rows == 1){                    
                    $stmt->bind_result($username, $hashed_password);
                    if($stmt->fetch()){
                        if(password_verify($password, $hashed_password)){
                            session_start(); // Session started
                            $_SESSION['username'] = $username;      
                            header("location: index.php");
                        } else{
                            $password_err = 'The password you entered was not valid.';
                        }
                    }
                } else{
                    $username_err = 'No account found with that username.';
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        $stmt->close();
    }
    $conn->close();
}

0 个答案:

没有答案