CodeIgniter Forums
Login redirect - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Login redirect (/thread-6759.html)



Login redirect - El Forum - 03-10-2008

[eluser]Firestorm ZERO[/eluser]
I'm just wondering if this is best way to implement this. Opinions needed.

Basically if a user accesses a page that is requires to login, the user is redirected to the login page but after login, I want the user to be redirected back to the page he wanted.

So in my check_permissions I have the following...

Code:
if ($this->session->userdata('logged_in') == FALSE)
{
   $this->session->set_flashdata('return_url', $_SERVER['REQUEST_URI']);   // keep url before redirect
   redirect('login', 'location');
}

And in the login page I basically have (pseudocode) ...

Code:
if login failed
   if $this->session->flashdata('return_url')
      $this->session->keep_flashdata('return_url');    // keep url in flashdata
   view->login form
else
   // Success login
   $this->session->set_userdata('logged_in');

   if $this->session->flashdata('return_url')
      redirect($this->session->flashdata('return_url')
   else
      redirect('main', 'location');



Login redirect - El Forum - 03-10-2008

[eluser]Seppo[/eluser]
The idea seems correct. However, insted fo $_SERVER['REQUEST_URI'] you should use $this->uri->uri_string() method.


Login redirect - El Forum - 03-10-2008

[eluser]Firestorm ZERO[/eluser]
Ah yeah. That's the function I was looking for. I remember it had it but forgot the name. Thanks.