繁体   English   中英

如何在登录Codeigniter / Tank_Auth时设置返回URL?

[英]How do I set a return URL during login on Codeigniter / Tank_Auth?

这里的问题是,当我的用户登录到我的应用程序时,他们总是被重定向到默认控制器。

我希望在登录之前将用户重定向到他们所在的页面。

因此,例如,如果用户正在阅读论坛帖子#12(阅读不需要登录),然后决定发布答案(回答需要登录),则他们登录后应该返回帖子#12。

我正在使用PHP / Codeigniter 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,也欢迎任何路线图。

我最近在我正在工作的网页上实施了该解决方案。

controller / 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
}

views / auth / login_form.php中,并利用CodeIgniter的user_agent库,添加一个隐藏的输入标记 ,该标记将包含引荐来源网址 ,如下所示:

<?=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'));
    }
}

这对我来说非常有效...很好而且很简单。 我相信它可以帮助您。

让我知道任何事情。

这是我一直在使用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
}

修改的Tank Auth登录功能(controllers / 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文件(config / routes.php)中

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

嗨,我解决了如下

在您的控制器中

添加以下内容: $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    
}

在Tank Auth Controller中(controllers / 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()替换了$_SERVER['REQUEST_URI'] ,因为它允许您获取/controller/method/...etc。 稍后在tank auth控制器中重定向

那对我来说是完美的,@Cubed Eye说道:“如果您的网址中包含_,您可能需要将preg_replace更改为其他名称”

感谢@Cubed Eye

我希望这也会对其他人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM