简体   繁体   中英

Php txt database reading

I'm working on a flat-file based login session. I'm modifying it to require only MD5 password. This is the txt file containing users and passwords MD5 .

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

And this is a part of my php code.

/* 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()

MY PROBLEM: it gives me access ONLY with the last password in the list txt. The first and second passwords won't work.

Can you spot any mistake in the code guys?

I don't know PHP much but the issue appears to me from your code is in parseUserFile and is that you are looping through the file contents and in each iteration you are assigning.

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

So, in each iteration, userData array's same item (ie item with key 'password') is getting overwritten by the password value in that line. So, after it finishes with looping through all the items in the file, it holds only the value of last password.

Instead what you should be doing is this:

//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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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