Php 我该如何提高效率(而不是一次又一次地重复同样的事情)?

Php 我该如何提高效率(而不是一次又一次地重复同样的事情)?,php,Php,如何清理/提高PHP中“比较此字符串”部分的效率? <?php session_start(); $_SESSION['name'] = $_POST['name']; $_SESSION['pass'] = $_POST['password']; //read the contents of our password file. $myFile = "password.txt"; $fh = fopen($myFile,

如何清理/提高PHP中“比较此字符串”部分的效率?

    <?php 
session_start();

     $_SESSION['name']  = $_POST['name'];
     $_SESSION['pass']  = $_POST['password'];

     //read the contents of our password file.
     $myFile = "password.txt";
     $fh = fopen($myFile, 'r');
     $data = fgets($fh);
     fclose($fh);

     //echo the data from the text file - I've edited this out so it doesn't display the data
    //echo $data;

     //print out an HTML line break
     //print "<br>";

     //now we need to split our line of data from the text
     //file so that we can do the comparison.

     //split the text into an array
     $text = explode(":",$data);

     //echo the split user name  - I've edited this out so it doesn't display the data
     //echo $text[3];

     //print out an HTML line break
     print "<br>";

     //echo the split password  - I've edited this out so it doesn't display the data
     //echo $text[1];

     //assign the data to variables
     $good_name = $text[0].$text[2];
     $good_pass = $text[1].$text[3];

     //print out an HTML line break
     //print "<br>";

     //compare the strings
     if( $_SESSION['name'] === $text[0] && $_SESSION['pass'] === $text[1] or $_SESSION['name'] === $text[2] && $_SESSION['pass'] === $text[3] or  $_SESSION['name'] === $text[4] && $_SESSION['pass'] === $text[5]){
        //echo "That is the correct log-in information";
        header("Location: home.php");
     }else{
        echo "That is not the correct log-in information.";
     }
  ?>    

我不想每次添加新用户时都像当前代码中所做的那样添加一个全新的实例。有什么方法可以提高效率吗?

可能就是您想要的

根据您对问题的评论,以下是我思考的一些示例代码:

$file = 'user:password:admin:password:Me:MyPass';
$name = preg_quote($_SESSION['name']);
$pass = preg_quote($_SESSION['pass']);
preg_match("/" . $name . ":" . $pass . "/", $file);
您必须立即解析文件的全部内容。这里也应该有所帮助

但总体上:<强>绝对/不强>存储明文密码。您应该用密码强散列算法散列它们。不要使用MD5,不要使用SHA。考虑使用BCRIPT、ScRyPT或PBDKF2,成本高,最好是盐。


不管怎样:使用数据库,它们会让你的生活更轻松;)

首先:不要存储明文密码。不要存储在任何数据库中,当然也不要存储在文本文件中

因此,我从您的评论中收集到的是,您的文件中有一行文本,其中包含由冒号分隔的用户名和密码的交替字段

在这种情况下:

$data = file_get_contents("password.txt");
$data = explode(":", $data);

if (count($data) % 2 != 0)  // Error, no even number of fields in password file
    return;

$loggedIn = false;
for ($i = 0; $i < count($data) / 2; $i += 2)
{
    $user = $data[$i];
    $pass = $data[$i + 1];

    if ($user == $_SESSION['name'] && $pass == $_SESSION['pass'])
    {
        $loggedIn = true;
        break;
    }
}
if ($loggedIn)
{
    // Do stuff
}
$data=file\u get\u contents(“password.txt”);
$data=分解(“:”,$data);
如果(count($data)%2!=0)//错误,密码文件中没有偶数个字段
返回;
$loggedIn=false;
对于($i=0;$i
我真的建议不要使用文本文件密码方法,并阅读一些MySQL/PDO教程


您还必须确保用户名/密码中不包含冒号。

最好重新排列您的
password.txt
,按
拆分用户,按
拆分用户名和密码,例如:
user1:pass1;user2:pass2
。然后您可以使用以下代码:

<?php
session_start();

//read the contents of our password file.
$myFile = "password.txt";
$fh = fopen($myFile, 'r');
$data = fgets($fh);
fclose($fh);

$temp = explode(';',$data); // split the file into user:pass
$users = array();
foreach( $temp as $userdata) {
    $user = explode(':', $userdata); // split username and pass
    $users[ $user[0] ] = $user[1]; // put it in an array
}


if( isset( $users[ $_POST['name'] ] ) and $users[ $_POST['name'] ] == $_POST['password']  ) {
     $_SESSION['name']  = $_POST['name'];
     $_SESSION['pass']  = $_POST['password'];
     header("Location: home.php");
}
else {
     echo "That is not the correct log-in information.";
}
?>

首先,从文件加载内容。内容将在每个
上拆分。然后我们循环到该数组的每一行,将其拆分为用户和密码

用户名和密码被放入一个新数组
$users

唯一剩下的就是检查数组中是否存在用户名,以及密码是否与之对应


登录后,您最好将数据放入会话中。

如果您想继续使用文本文件:将文件的行读入数组,并使用
foreach
循环。这假设您的文件中每行有一对
用户名:hashedPass
。另外:我希望
$\u会话['pass']
以及文本文件本身只包含密码散列?否则只需使用真实数据库(mysql或类似数据库)并使用PDO进行查询。
password.txt
文件是如何填充的?它是这样填充的:user:password:admin:password:Me:MyPass它被分解为:“因此,每个用户名都是偶数,每个相关密码都是奇数。重复您之前的问题,您可以使用数据库。我如何将新数据放入会话中?”?呃,还有很多PHP需要学习……您已经用
$\u SESSION['name']=$\u POST['name']对其进行了介绍。但我建议只在登录后才这样做。否则,如果登录失败,数据已经存储在会话中。因此,我尝试了此代码,但它不起作用,因为它希望用户名和密码是相同的。因此,只有当我输入user:user而不是user:pass时,它才允许登录。我认为这是因为在第17行(如果是isset)中进行了名为Pass的比较,有没有办法让它这样做:“如果用户输入的用户名与文件中的用户名相同,并且用户输入的密码与文件中该用户的密码相同,那么重定向到home.php”?是的!现在太完美了!尽管很多人都建议,我现在也应该学习mySQL了:p非常感谢!是的,将加密密码保存在数据库中更安全。它也更加灵活。祝你学习顺利:)