Jump to content

[SOLVED] Login Wont Work


TheJoey

Recommended Posts

<?php 
session_start();
$handle = fopen("users.txt", "r");
$valid = false;
while ($userinfo = fscanf($handle, "%s:%s\n")) {
    list ($name, $pass) = $userinfo;
    if ($username == $name && $password == $pass) {
        $valid = true;
    }
}
fclose($handle);
if ($valid) {
    $_SESSION['logged_in'] = true;
   echo 'yay ur in';
}
else
{
echo 'You entered a wrong password';
}

?>

 

im always getting "you entered a wrong password"

Link to comment
https://forums.phpfreaks.com/topic/172793-solved-login-wont-work/
Share on other sites

Where in the posted code are you setting $username and $password? Computers only do exactly what their code tells them to do. If there is no code that sets those variables there is no way they will have a value compare with what is read from the users.txt file.

 

Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini so that php would help you by pointing out variables that don't exist?

i have a html form which is sumbitting this code,

<?php 
echo '<table>
<tr><td>Username<br>
	<form action="php.php" method="post">
		<input name="username" type="text"></td></tr>
<tr><td>Password<br>
	<input name="password" type="password"></td></tr>
<tr><td><input value="Submit" type="submit"></td></tr>
	<tr><td><a href="register/index.php">Register</a></td></tr>
	</table>';
$username = $_POST['username'];
$password = $_POST['password'];
?>

 

i have added error_reporting(E_ALL);

to top of my page

 

 

    if ($username == $name && $password == $pass)

doesnt seem to like that code

That would imply that your comparison: if ($username == $name && $password == $pass) { is failing. What have you done to troubleshoot why it is failing? (since we don't have access to your "users.txt" file, it is a little hard for anyone but yourself to troubleshoot what your code and data are doing.) Have you checked if what is in $name and $pass is what you expect?

$username = $_POST['username'];
$password = $_POST['password'];

 

This should go in the page processing the form data, not the page creating the form.

 

<?php 
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$handle = fopen("users.txt", "r");
$valid = false;
while ($userinfo = fscanf($handle, "%s:%s\n")) {
    list ($name, $pass) = $userinfo;
    if ($username == $name && $password == $pass) {
        $valid = true;
    }
}
fclose($handle);
if ($valid) {
    $_SESSION['logged_in'] = true;
   echo 'yay ur in';
}
else
{
echo 'You entered a wrong password';
}

?>

 

You should also check to make sure the info is sent (among other validation tasks)

 

if (isset($_POST['username']))
{
$username = $_POST['username'];
}

If you check what is in $name and $pass like someone suggested you will find that fscanf() only does simple parsing and "%s:%s\n" does not separate strings at the ":" because the ":" is not white-space that terminates a string.

 

You will need to use a different separator, like a space or a tab to get fscanf() to do what you want.

im sorry im quiet new and doing my best to try and understand this.

What else could i use besides fscanf()?

so if i was to just use space i would set it out as '%s %s\n'?

 

edit: can comfirm a space works but this isnt going to work as im seperating my users with a : is there no other way?

<?php 
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$lines = file("users.txt",FILE_IGNORE_NEW_LINES); // read the lines into an array
$find = "$username:$password"; // form a string like you expect it to be in the array
if(in_array($find,$lines)){
$_SESSION['logged_in'] = true;
echo 'yay ur in';
} else {
echo 'You entered a wrong password';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.