简体   繁体   English

如何遍历php中的文本文件以查找特定的文本行

[英]how to loop through a text file in php to find a specific line of text

I have made a program that lets you enter the username and password then it stores it in a text file and when i want to login i want to make it so it loops through the text file that has the usr/pass in to find if you entered your credentials in correctly.我制作了一个程序,让您输入用户名和密码,然后将其存储在一个文本文件中,当我想登录时,我想让它循环遍历包含 usr/pass 的文本文件,以查找您是否正确输入您的凭据。 I'm not sure how to do this.我不知道该怎么做。 please can somebody help请有人帮忙

Example text.txt file:示例 text.txt 文件:

$ cat text.txt $猫文本.txt

text1|answer1
text2|answer2
text3|answer3

Example code:示例代码:

cat test2.php猫 test2.php

 <?php $text="text1"; $file="text.txt"; $f = fopen($file, 'r'); while($data = fgets($f)) { $ar_data=explode('|',$data); if($ar_data[0]==$text) { echo "looking for: ".$ar_data[1]."\\n"; } }

Example usage: $ php test2.php looking for: answer1示例用法: $ php test2.php looking for: answer1

The text fileis not a good way for verify user credentials.文本文件不是验证用户凭据的好方法。 You should try sql database.你应该试试sql数据库。 sqlite3 for example.例如 sqlite3。

You can just hard code them in an array:您可以将它们硬编码在一个数组中:

 $passwords = ['someuser' => 'password'];

If you really want to store them in a file, so you can change them (for example) without editing the code, One way is to use something like this:如果你真的想将它们存储在一个文件中,这样你就可以在不编辑代码的情况下更改它们(例如),一种方法是使用这样的东西:

 $passwords = ['someuser' => 'password'];
 file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');

This will create a file with something this in it (white space not withstanding):这将创建一个包含此内容的文件(不包括空格):

 <?php return array('someuser' => 'password');

Then when you need to import it into code you can simply use然后当您需要将其导入代码时,您可以简单地使用

 $passwords = require 'passwords.php';

Which will put the contents of that file into that variable.它将将该文件的内容放入该变量中。 Then you can check them really easily like so:然后你可以像这样很容易地检查它们:

$passwords = require 'passwords.php';
if(isset($passwords[$user]) && $passwords[$user] == $password){
    //do something when logged in
}

You can also modify the array and then save it:你也可以修改数组然后保存:

$passwords = require 'passwords.php';

$passwords['someuser'] = $new_password;

file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');

Of course you can even edit the passwords manually in the file.当然,您甚至可以在文件中手动编辑密码。 Sort of like a config file.有点像配置文件。

As I mentioned in the comments, it's better to use the DB, encryption and what not but as you said正如我在评论中提到的那样,最好使用数据库、加密以及您所说的其他内容

this is only for me and someone else这仅适用于我和其他人

As long as you don't have any third party data, and your ok with the security implications of this, then you can probably squeak by with the above.只要您没有任何第三方数据,并且您对此的安全影响没有问题,那么您就可以通过上述内容吱吱作响。

To explain it:解释一下:

Var Export converts arrays to valid PHP code, but in a string format. Var Export 将数组转换为有效的 PHP 代码,但采用字符串格式。 Then if we add the PHP open tag <?php , the return call return and the ending ;然后,如果我们添加 PHP 开始标记<?php ,则返回调用return和结尾; to it and save it in a PHP file, we now have a valid PHP file with dynamic passwords saved in it as an array.并将其保存在一个 PHP 文件中,我们现在有一个有效的 PHP 文件,其中包含动态密码作为数组保存在其中。

When you have such a file that returns an array, you can inject that into a variable just by setting it like I showed above.当您有这样一个返回数组的文件时,您可以将它注入到一个变量中,就像我上面展示的那样设置它。 Then it's a simple matter of checking to see if everything matches up.然后就是检查是否一切都匹配的简单问题。

Performance wise your offloading most of the penalty of this to saving the file, importing an array like this is very fast as is the key lookup.在性能方面,您将大部分损失卸载到保存文件中,导入这样的数组非常快,键查找也是如此。 Much faster then iterating though a file and trying to parse the data from it.比遍历文件并尝试从中解析数据要快得多。

You'll have to get the paths and filenames right and all that Jazz, but it should be pretty strait forward.您必须获得正确的路径和文件名以及所有 Jazz,但它应该非常严格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM