2009-02-14 81 views
0

出於某種原因,登錄我的網站必須進行兩次才能工作。如果任何人有任何想法,爲什麼我欣賞它。我必須登錄兩次

這裏是我對授權的代碼:

<?php 
session_start(); 
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 
require_once(SITE_ROOT.'includes/exceptions.php'); 
require_once(SITE_ROOT.'data/model.php'); 

/* 
* The purpose of this class is to manage 
* access to the application, making sure the 
* users are logged in before they can access 
* certain features 
*/ 

class Auth extends Model 
{ 
    function isUserLoggedIn() 
    { 
     /* 
     * Check for the user_id in $_SESSION 
     * and see if it's the database. Return 
     * true or false 
     * 
     */ 

     if(isset($_SESSION['user'])) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 

    } 

    static function redirectToLogin() 
    { 
     header("location: http://". DOMAIN .APP_DIR . "index.php?action=login"); 
    } 

    static function redirectToMain() 
    { 
     header("location: http://". DOMAIN . APP_DIR . "index.php?action=main"); 
    } 

    static function login($user) 
    { 
     /* 
     * Authenticate the user passing to the function 
     * a instance of the User object 
     */ 

     try 
     { 
      $db = parent::getConnection(); 
      $pass = $user->getPassword(); 
      $query = "select username, password from users where username = '".$user->getUsername()."' and password = '".$user->getPassword()."'"; 
      $results = $db->query($query);    

      if(empty($results)) { 
       throw new Exception('There was a problem logging you in', EX_LOGIN_ERROR); 
      }    

      $row = $results->fetch_assoc();   

      $user = $row['username']; 
      $_SESSION['user'] = $user; 

     } 
     catch(Exception $e){ 
      throw $e; 
     } 
    } 

    static function logout() 
    { 
     $old_user = $_SESSION['user']; 
     unset($_SESSION['user']); 
     session_destroy(); 
    } 

} 
?> 

THX

+0

嘗試在第一次登錄後刷新頁面(不要重新輸入數據;只需在URL欄上按ENTER鍵)。它會讓你登錄嗎?如果是這樣,你的bug是在其他地方(可能在你的控制器)。 – strager 2009-02-14 21:49:54

回答

4

我會聽@strager爲你的代碼,從我有限的PHP exerience,似乎沒有顯示任何東西那會導致錯誤。雖然我不禁提供無關你的問題的一些簡單的重構,但它只是讓我感覺好:

<?php 
    session_start(); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 
    require_once(SITE_ROOT.'includes/exceptions.php'); 
    require_once(SITE_ROOT.'data/model.php'); 

    /* 
    * The purpose of this class is to manage 
    * access to the application, making sure the 
    * users are logged in before they can access 
    * certain features 
    */ 

    class Auth extends Model 
    { 
     function isUserLoggedIn() 
     { 
      /* 
      * Check for the user_id in $_SESSION 
      * and see if it's the database. Return 
      * true or false 
      * 
      */ 

      return isset($_SESSION['user']); 
     } 

     static function redirectToLogin() 
     { 
      header("location: http://". DOMAIN .APP_DIR . "index.php?action=login"); 
     } 

     static function redirectToMain() 
     { 
      header("location: http://". DOMAIN . APP_DIR . "index.php?action=main"); 
     } 

     static function login($user) 
     { 
      /* 
      * Authenticate the user passing to the function 
      * a instance of the User object 
      */ 

      $db = parent::getConnection(); 
      $pass = $user->getPassword(); // replaced getPassword in the query with this variable, else there is no need to set it here. 
      $query = "select username, password from users where username = '".$user->getUsername()."' and password = '".$pass."'"; 
      $results = $db->query($query);    

      if(empty($results)) { 
       throw new Exception('There was a problem logging you in', EX_LOGIN_ERROR); 
      }    

      $row = $results->fetch_assoc();   
      $_SESSION['user'] = $row['username']; 

      // Why bother surrounding with try...catch just to throw the same exception 
     } 

     static function logout() 
     { 
      // what is $old_user used for? I can't see it set as a global variable anywhere 
      $old_user = $_SESSION['user']; 
      unset($_SESSION['user']); 
      session_destroy(); 
     } 

    } 
    ?> 
+0

謝謝杜德實際上是其他人的代碼,我試圖找出它的工作。 – jcslzr 2009-02-14 22:23:44

+0

我改正了你告訴我的,現在它可以正常工作,謝謝 – jcslzr 2009-02-14 22:29:33

1

那裏只是沒有足夠的代碼爲我們針點錯誤。問題可能與您的網站設計有關,您的登錄狀態信息是在登錄處理之前發送的。如果沒有,那麼我不知道這個信息有什麼問題。

0

看來你的問題已經回答了,但如果Web服務器從例如自動重新定向也可以出現問題:

yourdomain.com

WWW .yourdomain.com

或其他方式。

0

它不是由重定向引起的。應始終在設置或接收會話變量之前使用Session_start()。即它需要在類方法中。

相關問題