douniao7308 2016-10-30 14:47
浏览 92

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

I wanted to create a login form in which the username and password entered are verified against a text file stored content.

My index file 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.

This will basically get the login form 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>

Now instead of checking the array for username and password I want to check it against the text file login.txt with content

    Alex,123456
  • 写回答

1条回答 默认 最新

  • donglou1866 2016-10-30 15:03
    关注

    You can use get_file_contents() to load the entire file into memory. Then use preg_match_all() to find the occurrences of the username. preg_match_all('/'.$uname.',(.+)/',$uname, $matches). The $matches will hold a multi-dimensional array of all the returned results. You will then need to loop through this array to find if there is a matching password for each returned user. See https://regex101.com/r/PvSo22/1 for example

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计