1

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

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

例如,如果用户正在阅读论坛帖子#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。

4

3 回答 3

8

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

控制器/身份验证文件中添加对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 库添加一个隐藏的输入标签,该标签将包含以下引用 url

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

这对我很有用......它很好而且简单。我相信它可以帮助你。

让我知道任何事情。

于 2012-10-29T04:46:24.250 回答
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
}

修改 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 更改为其他内容,我只是使用它,因为它适用于我

编辑

我已经更新了这个功能,这是来自另一个项目的一个,我们对 tank auth 的东西进行了大量修改,所以如果东西有点不同,我很抱歉

至于传递 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
于 2011-09-14T13:38:17.597 回答
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    
}

在 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'])) {
         // success
         $decoded_uri = preg_replace('"_"','/',$return_to);
         redirect($decoded_uri);
    }
}
}

$_SERVER['REQUEST_URI']用这个替换了,$this->uri->uri_string()因为它可以让你得到 /controller/method/...等。稍后在 tank auth 控制器中重定向

这对我来说是完美的工作以及@Cubed Eye 所说的“如果您的网址中有 _,您可能需要将 preg_replace 更改为其他内容”

感谢@Cubed Eye

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

于 2014-11-17T02:44:54.817 回答