2011-06-28 32 views
0

我有2個PHP網站在同一臺機器。第一個站點(遺留系統)具有基本身份驗證:檢查是否設置爲$_SESSION['user_id']。我正在第二個網站(基於Kohana 3.1)工作,這將擴展第一個網站的功能。 這兩個網站都會互相鏈接,所以我需要在這些系統之間共享會話。這兩個網站使用相同的數據庫。用戶將在第一個站點登錄。 在我的網站中,我有一個代碼可以檢測到第一個的$_SESSION['user_id'],但我遇到了使用Kohana-Auth模塊保留會話的問題。分享2個網站之間的會話:一個傳統的PHP和一個Kohana 3.1網站

的第一個網站(傳統的)檢查會話是這樣的:

<?php 
session_start(); 
if(empty($_SESSION['user_id']))header("Location: index.php?action=3"); 
... //more dark code 

這是所有PHP文件......很多文件。

在我的Kohana站點中,我有一個控制器,在任何操作檢查會話之前。

<?php 

class My_Controller extends Controller_Template { 

    public function before() { 
     session_start(); 
     $this->auth = Auth::instance(); 
     if ($this->auth->logged_in()) { 
      //I have session in the second site... Do I have a session on the first one? 

      if (!isset($_SESSION['user_id']) || $_SESSION['user_id'] == "") { 
       //I have no session in the first site... I logout the user in my site 
       $controller = Request::current()->controller(); 
       if ($controller != 'auth') { 
        Request::current()->redirect('auth/logout'); 
       } 
      } 
      $this->user = ORM::factory('user', $this->auth->get_user()->id); 
     } else { 
      //I have no session in the second site... Do I have a session on the first one? 
      $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null; 
      if (isset($user_id)) { 
       $user = Model_User::get_user($user_id); 
       if ($user->loaded()) { 
        //I have session in the first site... I login the user in my site 
        $this->auth->force_login($user); 
        $this->user = ORM::factory('user', $this->auth->get_user()->id); 
       } 
      } 
      if (!$this->auth->logged_in()) { 
       //I still have no session => redirect to login of the first site 
       //Request::current()->redirect(...); 
       echo Debug::vars("BUUUU"); 
      } 
     } 
    } 

} 

此代碼是附近的工作:我可以從一個網站到另一個,並在檢測到用戶...但我意識到,當我的Kohana網站內型動物行爲之間的用戶navegates,在「登錄「用戶表增加。 這意味着在執行任何操作之前,「$this->auth->logged_in()」是FALSE ......這意味着Auth模塊不會在操作之間保留我的用戶,並且每次都執行強制登錄。

我不知道該怎麼辦。

我想檢測會議形式的第一個網站,但我不想在每次點擊登錄此用戶。

回答

1

我找到了答案! 在Kohana 3.1中,Kohana_Session類具有cookie的默認值。

/** 
* @var string cookie name 
*/ 
protected $_name = 'session'; 

該值與PHP會話的默認名稱不匹配:「PHPSESSID」。

並且通過在config目錄中創建一個名爲「session.php」的配置文件來修改該值。因此,我創建一個配置/ session.php文件是這樣的:

<?php defined('SYSPATH') or die('No direct script access.'); 

return array(
    'native' => array(
     'name' => 'PHPSESSID', 
    ) 
); 

我的最終控制人是這樣的:

<?php 

class My_Controller extends Controller_Template { 

    public function before() { 
     $this->auth = Auth::instance(); 
     if ($this->auth->logged_in()) { 
      //I have session in the second site... Do I have a session on the first one? 

      $user_id = Session::instance()->get('user_id'); 

      if (!isset($user_id) || $user_id == "") { 
       //I have no session in the first site... I logout the user in my site 
       $controller = Request::current()->controller(); 
       if ($controller != 'auth') { 
        Request::current()->redirect('auth/logout'); 
       } 
      } 
      $this->user = ORM::factory('user', $this->auth->get_user()->id); 
     } else { 
      //I have no session in the second site... Do I have a session on the first one? 

      $user_id = Session::instance()->get('user_id'); 

      if (isset($user_id) && $user_id != "") { 
       $user = Model_User::get_user($user_id); 
       if ($user->loaded()) { 
        //I have session in the first site... I login the user in my site 
        $this->auth->force_login($user); 
        $this->user = ORM::factory('user', $this->auth->get_user()->id); 
       } 
      } 
      if (!$this->auth->logged_in()) { 
       //I still have no session => redirect to login of the first site 
       //Request::current()->redirect(...); 
       echo Debug::vars("BUUUU"); 
      } 
     } 
    } 

} 

這一切......