PHP登录脚本无数据库 - 不同的页面重定向



我是PHP的新手,所以请赦免我缺乏知识。目前,我有一个没有数据库的PHP登录脚本。我只使用一个用户名和密码进行了测试,而且效果很好。但是,我需要添加另一组用户名和密码,它们应该重定向到不同页面。

php登录脚本:

<?php session_start(); /* Starts the session */
    /* Check Login form submitted */    
    if(isset($_POST['Submit'])){
        /* Define username and associated password array */
        $logins = array('Username1' => '123456', 'ReadOnly' => '1234567');
        /* Check and assign submitted Username and Password to new variable */
        $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
        $Password = isset($_POST['Password']) ? $_POST['Password'] : '';
        /* Check Username and Password existence in defined array */        
        if (isset($logins[$Username]) && $logins[$Username] == $Password){
            /* Success: Set session variables and redirect to Protected page  */
            $_SESSION['UserData']['Username']=$logins[$Username];
            $_SESSION['start'] = time(); // Taking logged in time.
            $_SESSION['expire'] = $_SESSION['start'] + (5400*60);
            // Ending a session in an hour from the starting time.
            header("location:index.php");
            exit;
        } else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
?>

页面中的脚本已重定向到:

<?php 
session_set_cookie_params(0);
session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
  header("location:login.php");
}
  else {
        $now = time(); // Checking the time now when home page starts.
        if ($now > $_SESSION['expire']) {
            session_destroy();
            echo "Your session has expired! <a href='http://localhost/PortalV4/login.php'>Login here</a>";
        }
        else { //Starting this else one [else1]
?>

当前,它仅将用户名'username1'和password'123456'重定向到index.php。我需要使用用户名" ReadOnly"和password'1234567'重定向到readonly.php。我如何比较不同的用户名和密码并重定向到不同页面?

您可以添加目标URL作为登录数组的一部分:

$logins = [
    'Username1' => 
        [
             'pass'       =>'123456',
             'destination'=>'index.php',
        ],
    'ReadOnly' => 
        [
             'pass'       =>'1234567',
             'destination'=>'read-only.php',
        ]
    ];
    /* Check Username and Password existence in defined array */        
    if (isset($logins[$Username]) && $logins[$Username]['pass'] == $Password){
        //session stuff here then
        header("location:" . $logins[$Username]['destination']);

最新更新