tags:

views:

39

answers:

0

Hello,

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?

Thanks in advance,

John

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 '<form name="login-form" id="login-form" method="post" action="./index.php"> 



    <div class="usernameformtext"><label title="Username">Username: </label></div> 
    <div class="usernameformfield"><input tabindex="1" accesskey="u" name="username" type="text" maxlength="30" id="username" /></div> 


    <div class="passwordformtext"><label title="Password">Password: </label></div> 
    <div class="passwordformfield"><input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" /></div> 


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

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


}

EDIT: Here is another function that is used.

function isLoggedIn()
{

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

    return false;

}

EDIT II: Here is another function that is used:

    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;
}