Nicholas Posted December 22, 2006 Share Posted December 22, 2006 When the user accesses index.php?action=login, $username and $password variables are defined through a form:[code]case 'login': print '<div class="para">' . '<h1>Please enter your username and password.</h1><br/>' . '<form action="http://www.ucjrd.com/index.php?action=redirect" method="post">' . '<table cellspacing="2" cellpadding="0" width="300">' . ' <tr>' . ' <td><strong>Username:</strong></td>' . ' <td><input type="text" size="12" maxlength="12" name="username" style="textbox">' . ' </tr>' . ' <tr>' . ' <td><strong>Password:</strong></td>' . ' <td><input type="password" size="12" maxlength="12" name="password" style="textbox">' . ' </tr>' . ' <tr>' . ' <td colspan="2" style="text-align: right;"><input type="submit" value="submit"></td>' . ' </tr>' . '</table>' . '</form>' . '</div>'; break; case 'redirect': $username = $_POST['username']; $password = $_POST['password']; admin($username,$password); break;[/code]Then the stored username and password are retrieved from a file and compared to the user's...[code]function admin($user,$pass) { $filename = "******.txt"; $file = fopen($filename,"r"); $set_user = fgets($file); $set_pass = fgets($file); fclose($file); if ($user == $set_user && $pass == $set_pass) { $_SESSION['logged_in'] = "true"; print '<div class="para">' . '<h1>Login Successful</h1><br/>' . 'Thank you for logging in. <a href="http://www.ucjrd.com/index.php?action=edit">Please click here to continue.</a>' . '</div>'; } else { $_SESSION['logged_in'] = "false"; print '<div class="para">' . '<h1>Login Error</h1><br/>'; print $_SESSION['logged_in'] . '<br/>'; if ($user == $set_user) { print 'Usernames are the same<br/>'; } else { print 'Usernames are not the same<br/>'; } if ($pass == $set_pass) { print 'Passwords are the same<br/>'; } else { print 'Passwords are not the same<br/>'; } print "Existing: $set_user and $set_pass<br/>" . "Entered: $user and $pass<br/><br/>" . 'There was an error when checking your username and password. <a href="http://www.ucjrd.com/index.php?action=login">Please review your username and password.</a>' . '</div>'; }[/code]Yet this yields the error message.As you can see, I've done a bit of debugging to narrow down my problems. The password is checked properly, and rings true when it is and false when it isn't. The username on the other hand, is always considered wrong.Am I missing something?Can anyone help me fix this annoying comparison issue?Thanks so much. ;D Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/ Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 You might want o look at using [url=http://php.net/trim]trim[/url] on any values retrieved from a file. One of the pifalls of using files v database. Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146278 Share on other sites More sharing options...
.josh Posted December 22, 2006 Share Posted December 22, 2006 you sure that $set_user has what it's supposed to have? echo out $set_user and $user so you can see what is actually being compared Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146280 Share on other sites More sharing options...
Nicholas Posted December 22, 2006 Author Share Posted December 22, 2006 The current combination of username and password is set to "test" for both, so I don't know why it would be affected differently...But you are indeed correct, works like a charm now.Thanks so much thorpe. ;D@ Crayon: read my code. I've printed both the variables entered and from the file, and checked them against each other. Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146284 Share on other sites More sharing options...
.josh Posted December 22, 2006 Share Posted December 22, 2006 okay i see where you printed them out, sorry. I guess if you had mentioned that they seemed to be printing out the same thing and yet not matching up, i would have suggested trim as well. I guess that's what you meant when you said "..is always considered wrong." ? Anyways, Good call thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146287 Share on other sites More sharing options...
Nicholas Posted December 22, 2006 Author Share Posted December 22, 2006 Sorry if I came across rudely.Meant no harm. You were trying to help me! Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146288 Share on other sites More sharing options...
.josh Posted December 22, 2006 Share Posted December 22, 2006 no offense taken. twas my bad. I should have paid more attention. Quote Link to comment https://forums.phpfreaks.com/topic/31565-solved-comparison-of-strings-not-correct/#findComment-146290 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.