2011-09-14 15 views
1

這裏的問題是,當我的用戶登錄到我的應用程序時,他們總是被重定向到默認控制器。如何在Codeigniter/Tank_Auth登錄時設置返回URL?

我想用戶重定向到他們在登錄前打開過的頁面。

因此,舉例來說,如果用戶正在閱讀的論壇帖子#12(閱讀不需要登錄),然後決定發佈答案(回答需要登錄),一旦他們登錄,他們應該回到帖子#12。

我使用PHP/2.0.2笨和Tank_Auth庫,並在我的幾個控制器的

function __construct() 
{ 
    parent::__construct(); 

    if (!$this->tank_auth->is_logged_in()) { 
     redirect('/auth/login/'); 
    } else { 

     //load stuff 
    } 

我的問題是

什麼是設置的最佳方式返回URL(Cookie?GET?),以及如何實現?

如果您熟悉Tank_Auth,我應該在哪些文件中進行這些更改?

即使您不使用Tank_Auth,也歡迎任何路線圖。

回答

4

這是我一直使用tank_auth的解決方案,它可能不是最好的,但我發現它適用於我。

在控制器

if (!$this->tank_auth->is_logged_in()){ 
    $encoded_uri = preg_replace('"/"', '_', $_SERVER['REQUEST_URI']); 
    redirect('/login/'.$encoded_uri);   
}elseif($this->tank_auth->is_logged_in(FALSE)){ // logged in, not activated 
    redirect('/user/reactivate/'); 
}else{ 
    //Logged IN Stuff Here 
} 

修改坦克驗證登錄功能(控制器/ auth.php)

function login($return_to = "") 
{ 
    if ($this->form_validation->run()) { 
     if ($this->tank_auth->login(
      $this->form_validation->set_value('login'), 
      $this->form_validation->set_value('password'), 
      $this->form_validation->set_value('remember'), 
      $data['login_by_username'], 
      $data['login_by_email'])) { 
      //...Other Stuff Here 
      $decoded_uri = preg_replace('"_"','/',$return_to); 
      redirect($decoded_uri); 
     } 
    } 
} 

您可能需要preg_replace函數改變,如果別的東西你網址有_,我只是用它,因爲它適用於我

編輯

我已經更新的功能,這是一個從我們大量修改坦克權威性的東西的另一個項目,因此,如果東西是有點不同,我很抱歉

對於傳遞encode_uri的東西,我已經添加了以下的routes.php文件文件(配置/ routes.php文件)

$route['auth/login/(:any)'] = 'auth/login/$1'; 
$route['auth/login'] = 'auth/login'; //Probably don't need this one now 
+0

這是一個有趣的解決方案 - 我看到''編碼_uri'放在'/ login /'後面 - 然後你的Tank_auth函數有一個名爲'$ return_to'的arg - 你怎麼通過' $ encoded_uri'到Tank_Auth函數? – pepe

+0

@torr,請參閱編輯。 –

+0

@ torr,我也爲你更新了路線' –

8

我最近實施的在網頁上我工作這一解決方案。

控制器/ AUTH文件的引用添加到USER_AGENT庫

function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('form', 'url')); 
    $this->load->library('form_validation'); 
    $this->load->library('security'); 
    $this->load->library('tank_auth'); 
    $this->lang->load('tank_auth'); 
    $this->load->library('user_agent'); //This is the line you are adding 
} 

視圖/ AUTH/login_form。該CodeIgniter的USER_AGENT庫的PHP趁着添加隱藏輸入標籤其中將包含引用網址如下:

<?=form_hidden('redirect_url', $this->agent->referrer());?> 
<?php echo form_submit('submit', 'Let me in'); ?> 
<?php echo form_close(); ?> 

之後,所有你需要做的就是重定向用戶命名輸入內容「REDIRECT_URL」用戶帖子登錄數據登錄操作

/** 
* Login user on the site 
* 
* @return void 
*/ 
function login() 
{ 
    /*.... Beginning of the login action function... 
     .... 
     .... 
    */ 

    if ($this->tank_auth->login(
       $this->form_validation->set_value('login'), 
       $this->form_validation->set_value('password'), 
       $this->form_validation->set_value('remember'), 
       $data['login_by_username'],$data['login_by_email'])) //valid 
    { 
     redirect($this->input->post('redirect_url')); 
    } 
} 

這對我來說很好...這很好,很簡單。我相信它可以幫助你。

讓我知道任何事情。

0

您好我解決了它如下

在你的控制器

補充一點:$this->load->library(array('tank_auth');

if (!$this->tank_auth->is_logged_in()) { 
    $encoded_uri = preg_replace('"/"', '_', $this->uri->uri_string()); 
    redirect('/auth/login/'.$encoded_uri); 
} else { 
// Logged IN Stuff Here  
} 

在坦克驗證控制器(控制器/ auth.php)

function login($return_to = "") 
{ 
if ($this->form_validation->run()) { 
    if ($this->tank_auth->login(
     $this->form_validation->set_value('login'), 
     $this->form_validation->set_value('password'), 
     $this->form_validation->set_value('remember'), 
     $data['login_by_username'], 
     $data['login_by_email'])) { 
     // success 
     $decoded_uri = preg_replace('"_"','/',$return_to); 
     redirect($decoded_uri); 
    } 
} 
} 

我將$_SERVER['REQUEST_URI']替換爲這個$this->uri->uri_string(),因爲這可以讓你獲得/controller/method/...etc。在罐AUTH控制器後重定向

這項工作非常適合我,怎麼說的@Cubed眼「您可能需要preg_replace函數改爲別的東西,如果您的網址_在他們」

感謝@Cubed眼

我希望這可以幫助別人。