2011-10-16 197 views
0

我想擁有它,所以如果我從我的網站的一部分登錄,它也會登錄到其他部分。Codeigniter單一網站登錄

一些注意事項:

  • 所有登錄連接到同一個數據庫...可以稱之爲DB1
  • 站點1的表phpfox
  • 站點2的表是vBulletin
  • 眼下它成功登錄到phpfox,但我無法弄清楚如何登錄論壇也....我已經添加了幾行代碼以嘗試做到這一點,但我不知道它:
  • 問題是我要保持我的網站的每個部分登錄(使用相同的用戶名和密碼)

這裏是我當前的代碼:

function login($username, $password, $passClean = null) 
{ 

    $this->faildLogins = new DB_FaildLogins(); 

    $ip = $this->input->ip_address(); 

    $faildLogins = $this->faildLogins->getFaildLoginsByIp($ip); 

    if($faildLogins){ 
     if($faildLogins->bannedTime > 0){ 
      $timeElapsed = ($faildLogins->lastTryDate + $faildLogins->bannedTime)-time(); 
      if($timeElapsed > 0){ 
       return sprintf('Your ip (%s) was banned for %s please try again after expire ban time!', $this->input->ip_address(), seconds2HumanTimeFormat($timeElapsed)); 
      } 
     } 
    } 

    $result = $this->user_model->get_login_info($username); 

    if ($result) { 
     if ($result->status == 'pending') { 
      return 'INACTIVE'; 
     } 

     if ($result->status == 'rejected') { 
      return 'REJECTED'; 
     } 


     if ($password === $result->password) { 
      $this->CI->session->set_userdata(array('id'=> $result->id)); 

      $this->user_model->addUserLogin($result->id); 

      $faildLogins = $this->faildLogins->getFaildLoginsByIp($ip); 
      if($faildLogins){ 
       $this->faildLogins->resetFaildLoginToIp($ip); 
      } 
      return TRUE; 

     // If passwords don't match 
     } else { 

     @mysql_connect('localhost', 'db1', 'db1_password') or die ("Can't connect to DB!"); 
     @mysql_connect('localhost', 'db1', 'db1_password', true) or die ("Can't connect to DB!"); 
     @mysql_select_db('phpfox') or die ("Can't select DB!"); 
     @mysql_select_db('vbulletin') or die ("Can't select DB!"); 

      $phpFoxUser = mysql_fetch_array(mysql_query("SELECT * FROM `phpfox_user` WHERE `user_name` = '{$username}'"), MYSQL_ASSOC); 

      if($phpFoxUser['user_name'] == $username AND 
        $phpFoxUser['email'] == $result->email AND 
        md5(md5($passClean).md5($phpFoxUser['password_salt'])) == $phpFoxUser['password']) { 

       $DBUsers = new DB_Users(); 
       $rows['id']    = $result->id; 
       $rows['password']  = md5($passClean); 

       if($DBUsers->saveIt($rows)) { 
        $this->CI->session->set_userdata(array('id'=> $result->id)); 
        return TRUE; 
       } else { 
        $this->faildLogins->addFaildLoginToIp($ip); 
        return FALSE; 
       } 
      } else { 
       $this->faildLogins->addFaildLoginToIp($ip); 
       return FALSE; 
      } 
     } 

    } else { 
     @mysql_connect('localhost', 'db1', 'db1_password') or die ("Can't connect to DB!"); 
     @mysql_connect('localhost', 'db1', 'db1_password', true) or die ("Can't connect to DB!"); 
     @mysql_select_db('phpfox') or die ("Can't select DB!"); 
     @mysql_select_db('vbulletin') or die ("Can't select DB!"); 

     $result = mysql_query("SELECT * FROM `phpfox_user` WHERE `user_name` = '{$username}'"); 
     $phpFoxUser = mysql_fetch_array($result, MYSQL_ASSOC); 

     if($phpFoxUser['user_name'] == $username AND md5(md5($passClean).md5($phpFoxUser['password_salt'])) == $phpFoxUser['password']) { 

      $DBUsers = new DB_Users(); 
      $rows['username']   = $phpFoxUser['user_name']; 
      $rows['password']   = md5($passClean); 
      $rows['usergroup']   = 'user'; 
      $rows['email']    = $phpFoxUser['email']; 
      $rows['activationCode']  = md5(time()); 
      $rows['status']    = 'approved'; 
      $rows['registerDate']  = time(); 
      $rows['registerIp']   = $this->input->ip_address(); 
      $rows['hash']    = uniqid(rand().rand().rand(), true); 

      $newUserId = $DBUsers->saveIt($rows); 

      if($newUserId) { 
       $this->CI->session->set_userdata(array('id'=> $newUserId)); 
       return TRUE;      
      } else { 
       return false; 
      } 


     } else { 
      $this->faildLogins->addFaildLoginToIp($ip); 
      return FALSE; 
     } 

     //md5(md5($sPassword) . md5($sSalt)) 
    } 

    $this->faildLogins->addFaildLoginToIp($ip); 
    return FALSE; 
} 

回答

1

設置會話變量,以獨特的東西給用戶,像userid。

$_SESSION['UserId'] = $id; 

然後,檢查登錄函數頂部的會話變量。

if (isset($_SESSION['UserId']) // user already logged in 
    $ret = 'ACTIVE'; 

然後在你的函數的底部

return $ret; 

BTW:我會擺脫多重收益在功能和使用$ RET變量,在我的例子。另外,不要忘記,當用戶註銷時刪除您的會話變量:

unset($_SESSION['UserId']); 

此外,您還可以在需要登錄的用戶的任何頁面的頂部檢查會話變量,並重定向到登錄頁面,如果沒有設置。