php读取文本文件并将其与用户输入进行比较

时间:2014-10-11 09:46:39

标签: php html file-io

我有一个名为login.txt的文本文件,我想将id和密码与用户的输入与保存在文本文件中的文件进行比较。我坚持使用php文件。任何帮助将不胜感激。

的login.txt

Brian,brianpass\n
Adam,adampass\n
Bob,bobpass\n

login.htm

<html lang="en">
<head>
<title>test</title>
</head>
<body>
<h1>Login Page</h1>
<form id="regform" method="get" action="login.php">
<p>Manager ID:<input type="text" name="mid" required="required" /></p>
<p>Password:<input type="password" name="mpassword" required="required" /></p>
<input type="submit" value="Login"/>
</form>
</body>
</html>

的login.php

<?php
$mid       = trim(@$_POST["mid"]);
$mpassword = trim(@$_POST["mpassword"]);

if ($mid != NULL && $mpassword != NULL)
{
    $str = file("login.txt");
    $newArray = array();
    foreach ($str as $value)
    {
        $array = (explode(",", $value));        
        $newArray[$array[0]] =  trim($array[1]);
    }
}
?>

问题:我希望系统确定登录过程是成功还是失败并回复它。

4 个答案:

答案 0 :(得分:2)

我不知道你为什么以这种方式保存密码(裸体),但由于你已经获得了每行爆炸,你可以比较它们:

if($array[0] == $mid && $array[1] == $mpassword) {
    // match
} else {
    // did not match
}

旁注:我建议至少使用散列函数。我建议在PHP 5.5的password_hash函数中使用mysql表,或者对于较小的PHP版本使用password compat

答案 1 :(得分:1)

好的,我就是这样做的。首先,您必须将表单方法从get更改为post

<强>的login.txt

Brian,brianpass
Adam,adampass
Bob,bobpass

test.php - 我这是一个单页应用,你可以根据需要拆分,但概念仍然相同

<?php

// Handle Post
if (count($_POST))
{
    // Parse login.txt
    $loginData = file('login.txt');
    $accessData = array();
    foreach ($loginData as $line) {
        list($username, $password) = explode(',', $line);
        $accessData[trim($username)] = trim($password);
    }

    // Parse form input
    $mid = isset($_POST['mid']) ? $_POST['mid'] : '';
    $mpassword = isset($_POST['mpassword']) ? $_POST['mpassword'] : '';

    // Check input versus login.txt data
    if (array_key_exists($mid, $accessData) && $mpassword == $accessData[$mid]) {
        echo "Username and Password is correct";
    } else {
        echo "Invalid username and/or password";
    }
}

?>

<html lang="en">
<head>
<title>test</title>
</head>
<body>
    <h1>Login Page</h1>
    <form id="regform" method="post" action="">
        <p>Manager ID:<input type="text" name="mid" required="required" /></p>
        <p>Password:<input type="password" name="mpassword" required="required" /></p>
        <input type="submit" value="Login"/>
    </form>
</body>
</html>

我测试了这个并且它有效。例如:

enter image description here

答案 2 :(得分:0)

您的解决方案存在安全问题并且很容易被利用,因此为了提高用户输入数据的安全性,请查看以下链接: enter link description here

用于比较写

foreach ($str as $value)
{
    $array = (explode(",", $value));        

    if(trime($array[0]) == $mid && trime($array[1]) == $mpassword) {
         // match
     } else {
       // did not match
         }
}

但是不建议这种方式用于登录系统,如果对使用数据库有限制,至少使用散列和加密解决方案,如RSA,hash,md5(推荐:它是单向)

答案 3 :(得分:0)

<?php
    $mid = @trim($_POST["mid"]);
    $mid = stripslashes($mid);
    $mpassword = @trim($_POST["mpassword"]);
    $mpassword = stripslashes($mpassword);
    $mid_pass = $mid.",".$mpassword."\n";//remove this \n if you remove it from file

    @override
    if ($mid != NULL && $mpassword != NULL)
    {
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
        for($i=0;$i<count($lines);$i++){
         if($mid_pass == $lines[$i]){
            echo "success";//Use your code here
            break;
         }else{
            echo "failed";//use your code here
         }
        }
    }
?>