I'm trying to make a simple login/reg, but the login doesn't work, only with the last line of my txt file, probably because of the other lines contain the '\n' on the end of every line, but it worked before, I don't know what went wrong... What do I have to do to make it work? Here's my code:
login.php:
<?php
if(isset($_POST["name"]) && isset($_POST["password"])){
$file = fopen('data.txt', 'r');
$good=false;
while(!feof($file)){
$line = fgets($file);
$array = explode(";",$line);
if($array[0]==$_POST["name"] && $array[1]==$_POST["password"]){
$good=true;
break;
}
}
if($good){
$message="Welcome";
}else{
$message="Try again";
}
include 'message.html';
fclose($file);
}else{
include 'login.html';
}
?>
reg.php:
<?php
if(isset($_POST["name"]) && isset($_POST["password"])){
$f=fopen("data.txt","a");
fputs($f,$_POST["name"].";".$_POST["password"]."\r\n");
fclose($f);
}else{
include 'reg.html';
}
?>
And this is how the data.txt looks like:
Viktor;1234
Eve;12345
Cathlin;12356
Use trim()
to strip away whitespaces from the beginning and end of the strings.
http://php.net/manual/en/function.trim.php
while(!feof($file)){
$line = fgets($file);
list($user, $pass) = explode(';', $line);
if(trim($user) == $_POST['name'] && trim($pass) == $_POST['password']){
$good=true;
break;
}
}