Reputation: 63
I have a problem with my webpage. I have this admin.txt file that contains "foo", and "bar" on the next line. Now on my webpage is a form where I must input two values, and when submitted will check if the values I inputted on the text inputs are equal with the values in the text file, but always returns fail even if i typed in the same values with the text file which is foo and bar. Check my codes:
index.php
<?php
<form method="post" action="check.php">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit">
?>
check.php
$location = "txt/admin.txt";
if ( file_exists( $location )) {
$page = join("",file("$location"));
$txt = explode("\n", $page);}
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
if ($text1==$txt[0] && $text2==$txt[1]){
echo "success.";
}
else
echo "fail";
Upvotes: 0
Views: 39
Reputation: 449613
There may be additional invisible line break characters that cause the comparison to fail.
In Windows for example, the line break is \r\n
, meaning your explode()
will leave the \r
in the data.
Always use trim()
on data coming from files.
Alternatively, you can also use the file()
function with the FILE_IGNORE_NEW_LINES
parameter.
Upvotes: 3