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()) {
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.
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