【问题标题】:Php txt database readingphp txt数据库读取
【发布时间】:2015-12-08 10:49:13
【问题描述】:

我正在开发基于平面文件的登录会话。我将其修改为仅需要 MD5 密码。这是包含用户和密码MD5的txt文件。

admin:5f4dcc3b5aa765d61d8327deb882cf99
user1:7c6a180b36896a0a8c02787eeafb0e4c
user2:6cb75f652a9b52798eb6cf2201057c73

这是我的 php 代码的一部分。

/* Bool validateLogin() returns TRUE if login/password are valid. Returns FALSE and sets $this->errorMessage if invalid or other error. */

   function validateLogin() {

   $this->errorMessage = '';
   $this->processLoginInput();
   if($this->parseUserFile()) {

         if( md5($_POST['password']) == $this->userData['password']) {

            $_SESSION['loginId'] = $_POST['password'];
            return(TRUE); }

         else { $this->errorMessage = "Invalid user name and/or password"; }       
   }    
   else { $this->errorMessage = "Unable to read user login data file"; }

      return(FALSE); 
} // end validateLogin()

/* Mixed parseUserFile(). Returns number of users in userFile, else FALSE */

   function parseUserFile() {

      $this->userData = array();
      if(is_readable($this->userFile)) {

         $lines = file($this->userFile);
         foreach($lines as $line) {

            $line = trim($line);
            if($line == "") { continue; }
            $parts = preg_split('/:/', trim($line));
            if(count($parts) >= 2) {

               list($user, $password) = $parts;
               $this->userData['password'] = $password; } } }

      return((count($this->userData)) ? count($this->userData) : FALSE );
}// end parseUserFile()

我的问题:它只允许我使用列表 txt 中的最后一个密码访问。 第一个和第二个密码不起作用。

你能发现代码中的任何错误吗?

【问题讨论】:

  • 你真的不应该使用 MD5 密码哈希,你真的应该使用 PHP 的 built-in functions 来处理密码安全。如果您使用的 PHP 版本低于 5.5,您可以使用 password_hash() compatibility pack
  • 嗨 jay :) 可以与 txt 数据库一起使用吗?我是 php 的菜鸟女孩,我不知道。我爱上了 txt 数据库 xxx

标签: php database text flat-file


【解决方案1】:

我不太了解 PHP,但从您的代码看来,问题在于 parseUserFile,并且您正在循环遍历文件内容并在您分配的每次迭代中。

$this->userData['password'] = $password; }

因此,在每次迭代中,userData 数组的相同项(即键为“密码”的项)将被该行中的密码值覆盖。因此,在完成循环遍历文件中的所有项目后,它只保存最后一个密码的值。

相反,你应该做的是:

//Pass in user name to fetch password for the user
function parseUserFile($userName) {

      $this->userData = array();
      if(is_readable($this->userFile)) {

         $lines = file($this->userFile);
         foreach($lines as $line) {

            $line = trim($line);
            if($line == "") { continue; }
            $parts = preg_split('/:/', trim($line));
            if(count($parts) >= 2) {

               list($user, $password) = $parts;
               //Get password for the user.
               if($user == $userName)
                   $this->userData['password'] = $password; } } }

      return((count($this->userData)) ? count($this->userData) : FALSE );
}// end parseUserFile()

【讨论】:

  • 对不起关系不好 :(
  • 你明白了它为什么会崩溃,我想你现在可以调试看看出了什么问题。问题是密码被覆盖了,它应该只是找到匹配用户的密码并退出功能......我没有设置运行 php。
猜你喜欢
  • 2015-04-26
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多