compare user and password with md5 in txt?

i have a php file:

<?php  
 if (isset($_POST['submit'])) { 
    $file_name = 'hash.txt'; 
    $user = md5($_POST['user']); 
    $password = md5($_POST['password']); 
    $handle = fopen('hash.txt',"r"); 
    $read_file = file($file_name);


    if ($user == $read_file[0] and $password == $read_file[1]) { 
        echo 'correct !'; 
    } else { 
        echo 'incorrect !'; 
    } 
} else { 
    echo 'please input something'; 
}
?> 
<body> 
<form action="file_handle3.php" method="post"> 
User<input type="text" name="user"/><br/> 
Password <input type="password" name="password"/> 
<input type="submit" name="submit" value="submit"/> 
    </body>

and the file txt : hash.txt the 1st line is hello the second is world

5d41402abc4b2a76b9719d911017c592

7d793037a0760186574b0282f2f435e7

i want to compare do value user input and convert them to md5 then compare with the one in txt file.i have no ideas why my code doesn't output a correct answer even when i type exactly the values (user:hello password:world).Sorry for my bad english

Upvotes: 1

Views: 355

Answers (1)

Evert
Evert

Reputation: 99495

When you use file() every line in the file becomes an item in the array.

However, every string in that array includes the newline (\n). So you need to make sure you do something like...

trim($read_file[0],"\r\n"); 

...first.

Upvotes: 1

Related Questions