Dynamic Redirect After Login In Codeigniter
Return to the page you were viewing after accessing a login page
Codeigniter provides all of the tools you might need to implement authentication on your website, but it would be a lot of work to create login pages, a user management system, a forgot my password feature, etc. Luckily, Ben Edmunds has developed a great third party authentication system for codeigniter called ion auth which is about as close to plug and play as it gets. The only thing I found necessary to do afterward was to create some method of sending unauthenticated users who access a password protected page to the login screen, and then redirect them back to whatever page they were trying to view previously after successfully logging in.
Here's some brief instructions on how to get Ion set up and configure the redirects.
The github page for Ion Auth has everything you need to know about installing it. Just copy the files to the appropriate directories in codeigniter (make sure not to overwrite any existing folders), and then just copy the appropriate sql from the SQL folder in the github repo (use ion_auth.sql if you're running a mysql database), and paste it into the SQL command prompt in your database to build the necessary tables for ion auth. Take note that if you're using codeigniter 3 you'll need to rename a couple files.
You may additionally want to add a route to your routes.php file that redirects http://site.com/auth to the auth/index view of the auth controller:
$route['auth'] = 'auth';
Now just head to http://site.com/auth and you should be automatically redirected to the auth/login view. Until you create additional admins, your login will be:
As a next step, you may want to pretty up these views in the auth folder with some of your own css. In my case, that meant just adding my own header and footer into the views, which I did by adding the following code to each view file (ex: login.php):