Reputation: 304
I am a login/register page and I have this for my login:
session_start();
$username = $_POST["username"];
$password = $_POST["password"];
$user_infile = "";
$password_infile = "";
$user_data = fopen("user_data.txt", "r");
while(! feof($user_data)){
$userArray = fgetcsv($user_data);
if ($userArray[0] == $username){
$user_infile = $userArray[0];
$password_infile = $userArray[1];
}
}
if (strval($password) == strval($password_infile)){
$_SESSION["loggedin"] = true;
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
header('Location: home.php');
exit();
}
else{
header('Location: error.php');
exit();
}
The login works perfect if the password is numeric i.e 123, but if password contains characters ie abc, the comparison fails and the else statement is executed. I used strval to convert both to string, but didnt work. I simply used password == password, that didnt work too. strcmp also didnt work. Am I missing something here?
Edit: This is the input file:
new, 123, 0
admin, pass, 0
jon, a1, 0
Upvotes: 0
Views: 88
Reputation: 1600
You should add quotes around your text field to be sure that fgetcsv
reads it all, then modify the fgetcsv
to include the character used as a text delimiter. In general CSV files should not contain extra spaces around the delimiter as these can be interpreted as being part of a field. Make sure you remove all spaces that are not part of the fields. You should also remove the strval
calls and simply compare the two values.
"new","123",0
"admin","pass",0
"jon","a1",0
$userArray = fgetcsv($user_data, 0, ',', '"');
if ($password == $password_infile) { // etc.
Upvotes: 1