I’m new to PHP and am having trouble with what seems like a relatively simple program. I can’t get my $_POST['Username']
to equal my $temp
and therefore never echo'success!';
even if i echo
every check and i can physically see that they equal, success never prints. Any help is greatly appreciated <3.
<?php if(isset($_POST['submit'])) { if (file_exists('logins.txt')) { echo 'The file was found' . '<br>'; $file = fopen('logins.txt', 'r'); while (feof($file) == false) { $temp = fgets($file, 20); if ($temp === $_POST['Username']) { echo'success!'; }else { echo 'failed' . '<br>'; echo $temp . '<br>'; echo $_POST['Username'] . '<br>'; } }; fclose($file); } } ?> <HTML> <body> <form action="" method="post"> Username <input type="text" name="Username" size="30" value=""> Password <input type="text" name="Password" size="30" value=""> <input type="submit" name="submit" value="Login"> </form> </body> </HTML>
Picture of the output using a textfile with the letters a – e
Advertisement
Answer
replace
if ($temp === $_POST['Username'])
with
if (trim($temp) == trim($_POST['Username']))
you probably have some space or something in your file
to better understand what is going on you can try to replace
echo $temp . '<br>'; echo $_POST['Username'] . '<br>';
with
var_dump($temp,$_POST['Username'])