Why doesn't validation in PHP?

There is a file users.php:
function user($login, $password){
 $file = file_get_contents(__DIR__ . '/users.txt');
 $users = explode("\n", $file);

 $all = array();
 foreach($users as $str){
 $temp = explode(' ', $str);
 if( $str[1] ) $all[$temp[0]] = $temp[1];
}

 if( $all[$login] == $password ) echo 'ok'; // Check false

 if( $all[$login] && $all[$login] == $password )
 return true;

 return false;
 }

And in the same directory as the file users.txt
admin pass
When I call the function with the Correct username and password returns me false result.
Wanted to know what the problem is, found out that the test does not pass. The screen was put ok but nothing.
When a separate output $all[ $login ] and $password, are identical to both (pass and pass), but the test is still false. Why?
July 9th 19 at 11:11
2 answers
July 9th 19 at 11:13
Solution
As a variant - extra white space in the file users.txt
But what's more likely -- the line feeds in users.txt - \r\n, and you share it only on \n
Accordingly, in the password gets the symbol \r
Check it out, you may withdrawing strlen of both passwords.
Thank you! Prior to this, never would have guessed... Very helpful! - Roxane_Jacobs commented on July 9th 19 at 11:16
July 9th 19 at 11:15
I'd line by line read the file and not downloaded the entire file into memory.
The file will not be too many rows. A maximum of 10 - Roxane_Jacobs commented on July 9th 19 at 11:18
then don't use file_get_contetnts use file - Roxane_Jacobs commented on July 9th 19 at 11:21
: what's the difference? - lucious16 commented on July 9th 19 at 11:24
: not razocharovala me ) - Roxane_Jacobs commented on July 9th 19 at 11:27
: php.net/manual/ru/function.file.php - lucious16 commented on July 9th 19 at 11:30
: hmm, thanks for the advice! - lucious16 commented on July 9th 19 at 11:33

Find more questions by tags PHP