Php 如何在登录Codeigniter/Tank_Auth时设置返回URL?
这里的问题是,当我的用户登录到我的应用程序时,他们总是被重定向到默认控制器 我希望用户在登录前被重定向到他们所在的页面 例如,如果用户正在阅读论坛帖子#12(阅读不需要登录),然后决定发布答案(回答需要登录),一旦登录,他们应该返回帖子#12 我正在使用PHP/Codeigniter 2.0.2和Tank_Auth库,并且在我的几个控制器中安装了Php 如何在登录Codeigniter/Tank_Auth时设置返回URL?,php,codeigniter,login,tankauth,returnurl,Php,Codeigniter,Login,Tankauth,Returnurl,这里的问题是,当我的用户登录到我的应用程序时,他们总是被重定向到默认控制器 我希望用户在登录前被重定向到他们所在的页面 例如,如果用户正在阅读论坛帖子#12(阅读不需要登录),然后决定发布答案(回答需要登录),一旦登录,他们应该返回帖子#12 我正在使用PHP/Codeigniter 2.0.2和Tank_Auth库,并且在我的几个控制器中安装了 function __construct() { parent::__construct(); if (!$this->tan
function __construct()
{
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
//load stuff
}
我的问题是
设置返回URL(Cookie?GET?)的最佳方法是什么,如何实现
如果您熟悉Tank_Auth,我应该在哪些文件中进行这些更改
欢迎使用任何路线图,即使您不使用Tank_Auth。这是我一直在使用的Tank_Auth解决方案,它可能不是最好的,但我发现它对我很有效 在控制器中 修改坦克认证登录函数(controllers/Auth.php) 如果您的URL中有,您可能需要将preg_替换更改为其他内容,我之所以使用它,是因为它对我有用 编辑 我已经更新了函数,这是另一个项目中的一个,我们对tank auth的内容进行了大量修改,所以如果内容有点不同,我很抱歉 至于传递encode_uri的内容,我在routes.php文件(config/routes.php)中添加了以下内容
这是我在tank_auth中使用的解决方案,它可能不是最好的,但我发现它对我很有效 在控制器中 修改坦克认证登录函数(controllers/Auth.php) 如果您的URL中有,您可能需要将preg_替换更改为其他内容,我之所以使用它,是因为它对我有用 编辑 我已经更新了函数,这是另一个项目中的一个,我们对tank auth的内容进行了大量修改,所以如果内容有点不同,我很抱歉 至于传递encode_uri的内容,我在routes.php文件(config/routes.php)中添加了以下内容
我最近在我工作的一个网页上实现了这个解决方案 在控制器/auth文件中添加对用户代理库的引用:
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
}
/**
* 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'));
}
}
在视图/auth/login\u form.php中,利用CodeIgniter的用户代理库添加一个隐藏的输入标记,该标记将包含引用者url,如下所示:
<?=form_hidden('redirect_url', $this->agent->referrer());?>
<?php echo form_submit('submit', 'Let me in'); ?>
<?php echo form_close(); ?>
这对我来说太好了。。。这很好,很简单。我相信它能帮助你
让我知道任何事情。我最近在一个我正在工作的网页上实现了这个解决方案 在控制器/auth文件中添加对用户代理库的引用:
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
}
/**
* 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'));
}
}
在视图/auth/login\u form.php中,利用CodeIgniter的用户代理库添加一个隐藏的输入标记,该标记将包含引用者url,如下所示:
<?=form_hidden('redirect_url', $this->agent->referrer());?>
<?php echo form_submit('submit', 'Let me in'); ?>
<?php echo form_close(); ?>
这对我来说太好了。。。这很好,很简单。我相信它能帮助你
告诉我任何事情。嗨,我解决了它如下 在控制器中 添加以下内容:
$this->load->library(数组('tank_auth');
罐内认证控制器(controllers/Auth.php)
我用这个$this->URI->URI\u string()
替换了$\u服务器['REQUEST\u URI']
,因为这允许您获取/controller/method/…以便稍后在tank auth controller中重定向
这对我来说非常合适,@Cubed Eye如何说“如果你的URL中有,你可能需要将preg\u替换为其他内容”
多亏了“立方眼”
我希望这对其他人也有帮助。嗨,我解决了它如下 在控制器中 添加以下内容:
$this->load->library(数组('tank_auth');
罐内认证控制器(controllers/Auth.php)
我用这个$this->URI->URI\u string()
替换了$\u服务器['REQUEST\u URI']
,因为这允许您获取/controller/method/…以便稍后在tank auth controller中重定向
这对我来说非常合适,@Cubed Eye如何说“如果你的URL中有,你可能需要将preg\u替换为其他内容”
多亏了“立方眼”
我希望这对其他人也有帮助。这是一个有趣的解决方案--我看到您将
$encoded\u uri
作为/login/
之后的一个段放置--然后您的Tank\u auth函数有一个名为$return\u to
的参数--您如何将$encoded\u uri
传递给Tank\u auth函数?这是一个有趣的解决方案--我看到您将$encoded\u uri
作为/login/
之后的一个段放置--然后您的Tank\u auth函数有一个名为$return\u to
--您如何将$encoded\u uri
传递给Tank\u 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
}
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);
}
}
}