2009-02-14 82 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()。即它需要在类方法中。

相关问题