Having to hit the site login button twice after first turning on computer
<p>I'm using the code below for a login.  It works fine, but when I first try logging in after turning on my computer, it only works the second time that I hit the "Login" button.  Any idea how I can make it not require hitting the "Login" button twice in this situation?</p>

<p>Thanks in advance,</p>

<p>John</p>

<pre><code>if (!isLoggedIn())
{

    if (isset($_POST['cmdlogin']))
    {

        if (checkLogin($_POST['username'], $_POST['password']))
        {
            show_userbox();
        } else
        {
            echo "Incorrect Login information !";
            show_loginform();
        }
    } else
    {

        show_loginform();
    }

} else
{

    show_userbox();
}


function show_loginform($disabled = false)
{

    echo '&lt;form name="login-form" id="login-form" method="post" action="./index.php"&gt; 



    &lt;div class="usernameformtext"&gt;&lt;label title="Username"&gt;Username: &lt;/label&gt;&lt;/div&gt; 
    &lt;div class="usernameformfield"&gt;&lt;input tabindex="1" accesskey="u" name="username" type="text" maxlength="30" id="username" /&gt;&lt;/div&gt; 


    &lt;div class="passwordformtext"&gt;&lt;label title="Password"&gt;Password: &lt;/label&gt;&lt;/div&gt; 
    &lt;div class="passwordformfield"&gt;&lt;input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" /&gt;&lt;/div&gt; 


    &lt;div class="registertext"&gt;&lt;a href="http://www...com/sandbox/register.php" title="Register"&gt;Register&lt;/a&gt;&lt;/div&gt; 
    &lt;div class="lostpasswordtext"&gt;&lt;a href="http://www...com/sandbox/lostpassword.php" title="Lost Password"&gt;Lost password?&lt;/a&gt;&lt;/div&gt; 

  &lt;p class="loginbutton"&gt;&lt;input tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login" ';
    if ($disabled == true)
    {
        echo 'disabled="disabled"';
    }
    echo ' /&gt;&lt;/p&gt;&lt;/form&gt;';


}
</code></pre>

<p>EDIT:  Here is another function that is used.</p>

<pre><code>function isLoggedIn()
{

    if (session_is_registered('loginid') &amp;&amp; session_is_registered('username'))
    {
        return true; // the user is loged in
    } else
    {
        return false; // not logged in
    }

    return false;

}
</code></pre>

<p>EDIT II:  Here is another function that is used:</p>

<pre><code>    function checkLogin($u, $p)
    {
    global $seed; 

        if (!valid_username($u) || !valid_password($p) || !user_exists($u))
        {
            return false; did not exist
        }


        $query = sprintf("
            SELECT loginid 
            FROM login 
            WHERE 
            username = '%s' AND password = '%s' 
            AND disabled = 0 AND activated = 1 
            LIMIT 1;", mysql_real_escape_string($u), mysql_real_escape_string(sha1($p . $seed)));
        $result = mysql_query($query);

        if (mysql_num_rows($result) != 1)
        {
            return false;
        } else
        {

            $row = mysql_fetch_array($result);

            $_SESSION['loginid'] = $row['loginid'];

            $_SESSION['username'] = $u;

            return true;
        }


 return false;
}
</code></pre>
