Jump to content

[SOLVED] Comparison of strings not correct?


Nicholas
 Share

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.