• Home
  • Contact
blog.biernacki.ca

Kuba's Online Workshop

  • Home
  • Contact

Redirect user back after login using CodeIgniter

01/06/2014 How To PHP Thoughts & Rants 3 Comments

So you’ve made your CodeIgniter app using PHP, you’ve made is safe, secure and skipped needing to use SQL in favor of Active Records. Well the one thing most people forget to do (or realize later) is a simple UX (user experience) flow of post login routing.

Basically, you want users to go back to the content they viewed prior to login, in most cases this is the most preferred route because the user logs in so they can post, comment, or interact with something that might only be for logged in / registered users.

The easy way to handle this using CodeIgniter is to do a simple session save of the referring url like so:

In your login controller:

function index() {
    $this->load->library('user_agent');  // load user agent library
    // save the redirect_back data from referral url (where user first was prior to login)
    $this->session->set_userdata('redirect_back', $this->agent->referrer() );  
    ...
}

Then once you’ve authenticated your user, you can check if that value exists in session, and redirect them back to their original page, all seamlessly. Notice how I grab the redirect_back value into a temp var and remove the session variable (in case of it sticking for another redirect):

// user is authenticated, lets see if there is a redirect:

if( $this->session->userdata('redirect_back') ) {
    $redirect_url = $this->session->userdata('redirect_back');  // grab value and put into a temp variable so we unset the session value
    $this->session->unset_userdata('redirect_back');
    redirect( $redirect_url );
}

So there you have it, a simple flow for login and post-login redirection. Enjoy!

codeigniterPHP

Codeigniter keeping bots out of your sessions table or how I cleaned up my sessions

Mounting remote SFTP/SSH shares on Mac OSX 10.9

Categories
  • Android
  • Apple
  • Coldfusion
  • Cool Apps
  • CSS
  • GitHub
  • Hardware
  • How To
  • In the News
  • Javascript
  • Linux
  • PHP
  • RC Hobby
  • Reviews
  • SysAdmin
  • Thoughts & Rants
  • Tinkering
Recent Comments
  • Jim on Fixing ONKYO RC-710m Remote volume issues
  • Tony on Fixing ONKYO RC-710m Remote volume issues
  • Robert Lawrence on Fixing ONKYO RC-710m Remote volume issues
  • Dasen on Fixing ONKYO RC-710m Remote volume issues
  • Ahmed on Fixing ONKYO RC-710m Remote volume issues
RollmyBlog
  • Twitter
  • WebMojo Design
  • Windsor Ultimate
Archives
  • June 2020
  • January 2015
  • June 2014
  • February 2014
  • January 2014
  • December 2013
  • May 2013
  • February 2013
  • July 2012
  • June 2012
  • February 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • November 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • June 2008
  • October 2007
  • September 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
Proudly powered by WordPress | Theme: Doo by ThemeVS.