Anyone can show me why this piece of code works with the second line but not the first line, although they are the same?
Here is the pass.txt file:
01234567 5f4dcc3b5aa765d61d8327deb882cf99
01234567 5f4dcc3b5aa765d61d8327deb882cf99
Here’s the code:
<?php
$f = fopen("pass.txt", "r");
if ($f) {
$buffer = fgets($f);
$buffer = fgets($f); //Comment out this line to read the first line
$token = explode(' ', $buffer);
if ($token[1] == hash('md5', 'password'))
echo "Password correct";
else
echo "Password incorrect";
}
?>
As @David Schwartz pointed out, the most possible reason is the newline character.
You could use
filefunction to ignore the newline char.If the file is very large, then use
fgetsto read it one by one and trim the newline.