Welcome Guest, Not a member yet? Register   Sign In
Access restrictions to pages using Session.

Hi all,

Now I am facing another issue with CI and is related to Session.. The task is about User Validation.. After login, the details are stored in Session..And I want to make it, so that only Logged In users can access some pages..

In normal php development, we can make it by using a function and call to that function in the restricted pages. Then the page will be redirected to login page, if the user doesn't have permission or not logged in.. How can I do this from inside the Controllers ?

Have any libraries available for this ?

Hi Aniesh,

in the CI wiki you can get more informations about that:

If you don't need more advanced settings like different user levels I can
recommend a tutorial written by Bramme. I just gave it a try and found
it very usefull. It's a good way to start and you can extend the class like
needed. Tutorial link:

There are a lot of threads about advanced libraries in this forum in case
Brammes library is too basic for you.

I extended the controller class and added a function called _weed_nonusers(). This function will check if the person is logged in and then redirect to the login page if they aren't. So whenever i have a function that is restricted access, I just add $this->_weed_nonusers() at the very beginning of the function.

Also, if you haven't changed your database class to one of hte user created ones in the wiki section that store session info in the database, then you should write a sentry class which validates the session cookie whenever you need to restrict access. Otherwise its easy for someone to go into the cookie and change admin=0 to admin=1 for instance.

Hi Friends,

After searching, I created a library for Session Validation. It works like this:

1. After a user login to the site, I added a variable 'logged_in' to Session.

2. Then on the page where it need to check the User Authentication, I just called the Session validation function to make sure that whether the user is logged in or not.

To do this:

Create a new library and save with the name: 'User_authentication.php'

Path to save the Library is: system/application/libraries/User_authentication.php

User_authentication.php file Content is:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

* Code Igniter AJAX Class
* This class enables you to check User Authentication with the help of Session.
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Libraries
* @author        Aniesh
* @link        http://www.no-link-yet.com

class User_authentication
    private $obj = null;

    function User_authentication()
        $this->obj =& get_instance();

    function user_validation()

        // Not logged in, then redirect to the Home Page.



Then load the Session and this User Authentication library to all pages. For this open the page:


Then search for the $autoload['libraries'] and add the following:

$autoload['libraries'] = array('User_authentication', 'session');

A sample Controller page, where I need to test the Use Authentications is:


class Mysubscription extends Controller
    function __construct()

                // This will do the Session Validation task.

        //  function add-your-functions-here()


Thank You.

Theme © iAndrew 2016 - Forum software by © MyBB