PHP使用Textfile登录

时间:2016-07-04 10:16:15

标签: php html forms

如何在填写表单中的数据后,为文本文件中的每个用户逐行显示数据? 我的数据应如下所示 文本文件中的示例输出:

Username: ABC Password: 123
Username: DEF Password: 456
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php
// define variables and set to empty values
$username = $password = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 $name = test_input($_POST["name"]);
 $password = test_input($_POST["password"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>How to show data in row by row for every users in textfiles after they filled out data from a form?</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
Password: <input type="text" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$myfile = fopen("login.txt", "a+") or die("Unable to open file!");
echo "<h2>Username:</h2>";
fwrite($myfile, $name);
echo "<br><h2>Password:</h2>";
fwrite($myfile, $password);
echo "<br><br>";
fclose($myfile);
?>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

好的,忽略了这样一个事实,即你正在玩的内容似乎有点怀疑,最好是糟糕的编程习惯:

限定符

  • echo将输出到屏幕而不是文本文件。
  • fwrite将写入文件,但不会输出到屏幕。
  • 请注意,如果文本文件与PHP文件不在同一目录中,则应填写完整路径。
  • 密码数据应 始终 进行哈希处理,不得保留为纯文本值。

保存数据

要将数据字符串保存到文件,您可以使用file_put_contents

... 
(rest of file)  
</form>
</body>
</html>
$data = "Username: ".$name." Password: ".$password.PHP_EOL; 
// PHP_EOL is a system specific end of line marker. 
// Alternative is to use the "\n" marker,
file_put_contents("login.txt",$data,FILE_APPEND) or die("Unable to save to file!");

就是这样,$data的值被附加到文本文件的末尾。如果您不想附加数据,而是覆盖文件中的数据,只需删除FILE_APPEND标志即可。

读取数据

您希望使用file_get_contents轻松加载数据,然后可以使用explode将其拆分为数组,以将每行读取为自己的值(可选,但建议),然后您可以使用foreach输出该值以处理文件的每一行(array):

$fileContents = file_get_contents("login.txt") or die("Unable to open file!");
$fileLines = explode(PHP_EOL,$fileContents);
foreach($fileLines as $line){
    //line now displays the data from the file, line by line. 
    print $line."<br>"; // this will output said data.
}
unset($line);

答案 1 :(得分:0)

此外,您的fwrite代码应位于if statement

    <!DOCTYPE HTML>
    <html>
    <body>
    <?php
    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    // define variables and set to empty values
    $username = $password = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $name = test_input($_POST["name"]);
     $password = test_input($_POST["password"]);

    $myfile = fopen("login.txt", "a+") or die("Unable to open file!");
   fwrite($myfile, "Username :" . $name." "."Password :".$password."\r\n");
   fclose($myfile);
    }
    ?>
    <h2>How to show data in row by row for every users in textfiles after they filled out data from a form?</h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name: <input type="text" name="name">
    <br><br>
    Password: <input type="text" name="password">
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form>

    </body>
    </html>

我使用\r\n作为新行,因为你有文本文件。