TheJoey Posted September 2, 2009 Share Posted September 2, 2009 <?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" Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2009 Share Posted September 2, 2009 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? Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 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 Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 im getting no errors at all just getting wrong password error. i moved post into the php code :S i have no idea why its doing that Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2009 Share Posted September 2, 2009 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? Quote Link to comment Share on other sites More sharing options...
methomps Posted September 2, 2009 Share Posted September 2, 2009 $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']; } Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 I currently have all that in the processing page, sorry if i miss typed it. ill give this a shot once i get home Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 Its still giving me wrong password. Mind you my users.txt is being stored as users.txt username:password username:password username:password Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2009 Share Posted September 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 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? Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 Could fgets do the required job? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2009 Share Posted September 2, 2009 <?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'; } ?> Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 2, 2009 Author Share Posted September 2, 2009 k i just need to try a new things but it should be ok. ive been told that only fwrite and most f() commands can only be used. ive never seen an example like that. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.