如何使用没有数据库的php身份验证显示特定的html页面?

时间:2016-06-13 20:25:00

标签: php html forms-authentication

作为php的新手,我正在尝试使用简单的php身份验证而不使用数据库来显示特定的html页面。我在登录数组中存储不同的用户名和密码。我想为每个用户名显示不同的页面。例如if isset[Username]= Marc header("location:marc.html")

的login.html

<form action="login.php" method="post" name="Login_Form">
  <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
    <?php if(isset($msg)){?>
    <tr>
      <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
    </tr>
    <?php } ?>
    <tr>
      <td colspan="2" align="left" valign="top"><h3>Client identification</h3></td>
    </tr>
    <tr>
      <td align="right" valign="top"></td>
      <td><input name="Username" type="text" placeholder="Username" class="Input"></td>
    </tr>
    <tr>
      <td align="right"></td>
      <td><input name="Password" type="password" placeholder="Password" class="Input"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Submit" type="submit" value="Enter" class="Button3"></td>
    </tr>
  </table>
</form>

的login.php

<?php session_start(); /* Starts the session */

    /* Check Login form submitted */    
    if(isset($_POST['Submit'])){

        /* Define username and associated password array */
        $logins = array(
                    'Marc' => 'pass','username1' => 'password1',
                    'Guy' => 'pass','username2' => 'password2',
                    'Lucie' => 'pass','username3' => 'password3',
                    'Eva' => 'pass','username4' => 'password4');

        /* 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];
            header("location:marc.html");
            exit;
        } else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

一眼就能看出你需要:

 header("location:marc.html");

更改为:

 $url = strtolower($Username);
 header("location:".$url.".html");