从文件中获取用户名和密码并验证(不带SQL)

时间:2016-10-30 14:47:54

标签: php

我想创建一个登录表单,其中输入的用户名和密码将根据文本文件存储的内容进行验证。

我的索引文件 index.php

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

    if(!isset($_SESSION['UserData']['Username'])){
        header("location:login.php");
        exit;
     }
  ?>

 Congratulation! You have logged into password protected page. <a href="logout.php">Click here</a> to Logout.

这基本上会获得登录表单 login.php             

            /* Check Login form submitted */    
            if(isset($_POST['Submit'])){
                /* Define username and associated password array */
                $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');

                /* 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:index.php");
                    exit;
                } else {
                    /*Unsuccessful attempt: Set error message */
                    $msg="<span style='color:red'>Invalid Login Details</span>";
                }
            }
        ?>
        <!doctype html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>PHP Login form</title>
        <link href="./css/style.css" rel="stylesheet">
        </head>
        <body>

        <br>
        <form action="" 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>Login</h3></td>
            </tr>
            <tr>
              <td align="right" valign="top">Username</td>
              <td><input name="Username" type="text" class="Input"></td>
            </tr>
            <tr>
              <td align="right">Password</td>
              <td><input name="Password" type="password" class="Input"></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input name="Submit" type="submit" value="Login" class="Button3"></td>
            </tr>
          </table>
        </form>
        </body>
        </html>

现在不是检查数组中的用户名和密码,而是要根据文本文件 login.txt 检查内容

    Alex,123456

1 个答案:

答案 0 :(得分:0)

您可以使用get_file_contents()将整个文件加载到内存中。然后使用preg_match_all()查找用户名的出现次数。 preg_match_all('/'.$uname.',(.+)/',$uname, $matches)$matches将保存所有返回结果的多维数组。然后,您需要遍历此数组以查找每个返回的用户是否存在匹配的密码。 例如,请参阅https://regex101.com/r/PvSo22/1