How do I set a return URL during login on Codeigniter / Tank_Auth?
The issue here is that when my user logs into my app, they always are redirected to the default controller.
I would like the user to be redirected to the page they were on before logging in.
So for example, if the user is reading forum post #12 (reading does not require login) and then decides to post an answer (answering requires login), once they login they should go back to post #12.
I am using PHP/Codeigniter 2.0.2 and the Tank_Auth library, and have in several of my controllers
function __construct()
{
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
开发者_StackOverflow社区 redirect('/auth/login/');
} else {
//load stuff
}
My question is
What is the best way to set a return URL (Cookie? GET?), and how would that be implemented?
If you're familiar with Tank_Auth, in which files should I make these changes?
Any roadmaps are welcome, even if you don't use Tank_Auth.
I Recently implemented this solution on a webpage I was working.
In the controller/auth file add a reference to the user_agent library:
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
}
In the views/auth/login_form.php and taking advantage of the CodeIgniter's user_agent library add a hidden input tag which will contain the referrer url as follows:
<?=form_hidden('redirect_url', $this->agent->referrer());?>
<?php echo form_submit('submit', 'Let me in'); ?>
<?php echo form_close(); ?>
After that, all you have to do is redirect the users to the content of the input named "redirect_url" when the user posts the login data to the login action:
/**
* 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'));
}
}
This works great for me... It's fine and simple. I believe it can help you.
Let me know about anything.
This is the solution I've been using with tank_auth, it's probably not the best, but I've found it works well for me.
In the controller
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
}
Modified Tank Auth Login Function (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);
}
}
}
You may need to change the preg_replace to something else if your urls have _ in them, I just used that because it works for me
EDIT
I've updated the function, this is one from another project that we heavily modified the tank auth stuff, so if stuff is a bit different, I'm sorry
As for the passing the encode_uri stuff, I've added the following to the routes.php file (config/routes.php)
$route['auth/login/(:any)'] = 'auth/login/$1';
$route['auth/login'] = 'auth/login'; //Probably don't need this one now
Hi I solved it as follows
In your controller
Add this: $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
}
In 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);
}
}
}
I replaced $_SERVER['REQUEST_URI']
with this $this->uri->uri_string()
because that allow you get /controller/method/...etc. to redirect later in tank auth controller
That work perfect for me and how said @Cubed Eye "You may need to change the preg_replace to something else if your urls have _ in them"
Thanks to @Cubed Eye
I hope this helps someone else too.
精彩评论