Wierd problem - members have to log in twice

Discussion in 'PHP' started by peppy, Aug 24, 2010.

  1. #1
    Greetings,

    Some of my members are complaining that they have to log into my site twice.

    When they log in the first time, they get the "login failed" message. They then press the back button and log in once again and it always works the second time.

    Here is my login page code (PHP and the HTML form). I'm sure it is probably something really simple but I am not catching it. Let me know if you see something:

    
    <?php
    include("connection.php"); 
    mysql_select_db("database");
    session_start();
    if(isset($_POST['login'])){
           $username = mysql_real_escape_string($_POST['username']);
           $password = mysql_real_escape_string($_POST['password']);
    	   $tUnixTime = time();
           $sGMTMySqlString = gmdate("Y-m-d H:i:s", $tUnixTime);
           if (!$username || !$password) {
              print "Please fill out all fields.";
              exit;
           }
           $logres = mysql_num_rows(mysql_query("SELECT * FROM members WHERE username = '$username' and password = '$password'"));
           if ($logres <= 0) {
              print "Login failed. If you have not already, please signup. Otherwise, check your spelling and login again.";
              exit;
           } 
           else {
           session_start();
           session_register('username');
           $_SESSION['username'] = $username;
           if (isset($_SESSION)) {
             echo'You are now logging in'; mysql_query("UPDATE members SET activity = '$sGMTMySqlString' WHERE username = '$username'");
           }
           else {
             echo "You are not logged in!";
           }  
           echo'<html><head><meta http-equiv="REFRESH" content="1;url=http://www.mysite.com/members/' . $_SESSION['username'] . '/"></head><body></body></html>';
           exit;
           }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login Page</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="SHORTCUT ICON" href="/favicon.ico" />
    </head>
    
    <body>
    <div class="container">
      <?php include("header.php"); ?>
      <?php include("sidebar.php"); ?>
      <div class="content">
        <center>
          <h1>Use the form below to log in:</h1>
        </center>
    <table width="330" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
          <tr>
            <td><form name="login" method="post" action="">
                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                  <tr>
                    <td colspan="3"><strong>Member Login </strong></td>
                  </tr>
                  <tr>
                    <td width="78">Username</td>
                    <td width="6">:</td>
                    <td width="294"><input name="username" type="text" id="username"></td>
                  </tr>
                  <tr>
                    <td>Password</td>
                    <td>:</td>
                    <td><input name="password" type="password" id="password"></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="login" value="login"></td>
                  </tr>
                  <tr>
                    <td colspan="3"><center><a href="http://www.mysite.com/forgotpassword.php">Forgot your Password or Username? Click Here</a></center></td>
                  </tr>
                </table>
              </form></td>
          </tr>
        </table>
        <br>
      </div>
    <?php include("footer.php"); ?>
    </div>
    </body>
    </html>
    
    PHP:

     
    peppy, Aug 24, 2010 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You're using session_start() twice which could cause problems.

    And don't use session_register()... It's a deprecated function.
     
    nico_swd, Aug 24, 2010 IP
  3. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Thanks for the reply.

    One thing I forgot to mention is that "session_start();" was not up at the top when the problem started, I added that last night when I was trying to troubleshoot. Should I just leave that up at the top and delete the one in the "esle {}" statement?

    I will delete the "session_register('username');" as you suggested.
     
    peppy, Aug 24, 2010 IP
  4. dfsweb

    dfsweb Active Member

    Messages:
    1,587
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    88
    #4
    You should just leave the one at the top and delete the 2 lines below else for session_start and session_register and see if that fixes the issue. The rest of the code seems fine and should really work!

    Some extra unneccessary info: On a completely unrelated note, I don't think mysql_num_rows can return < 0 so you could check for == 0 instead of <=0 when checking for number of rows returned.
     
    dfsweb, Aug 25, 2010 IP
  5. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Awesome nico_swd and desweb, this seems to have worked, but I will keep an eye on it.

    Rep added.
     
    peppy, Aug 25, 2010 IP