2010-03-17 54 views
0

我使用下面的代碼進行登錄。它工作正常,但是當我第一次嘗試登錄後打開我的電腦,它僅在第二次按下「登錄」按鈕時才起作用。任何想法如何讓它不需要在這種情況下兩次點擊「登錄」按鈕?第一次啓動計算機後必須兩次點擊網站登錄按鈕

由於提前,

約翰

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


} 

編輯:這是使用其他功能。

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; 

} 

編輯II:這是使用其他功能:

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

isLoggedIn()是做什麼的? – 2010-03-17 02:33:20

+0

函數isLoggedIn() { if(session_is_registered('loginid')&& session_is_registered('username')) { return true; //用戶被鎖定在 } else { return false; //未登錄 } return false; } – John 2010-03-17 02:36:39

回答

0

這是一個老問題,其中的一些功能甚至與最新的PHP刪除 - session_is_registered。但是我偶然發現了這個問題,我想對答案進行猜測。在第一種形式的登錄中,它設置了會話,但會話在show_user_box中被選中,導致它不顯示。會話設置後,雖然你可以登錄並看到用戶框?我希望每個功能都在這裏,所以我可以擁有所有工具來解決這個難題,儘管哈哈。看舊代碼很有趣。