How to Session handling in Codeigniter

View: 622    Dowload: 0   Comment: 0   Post by: hanhga   Category: Codeigniter   Fields: Other

I googled everywhere and checked for Session handling in Codeigniter only once for each controller but did not found any easy solution.In most of the article I have seen that you need to check for session in every function inside the controller. Checking for session value inside the controller in every function is very lengthy and hectic.
So finally I got the solution and sharing with you in this article. 
First you need to autoload the session library.

Open the application folder and click on the config folder and look for the autoload.php file. Now set the autoload library to

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

so that every time we run the controller session will autoload

Now set the user session at the time of login.
How to login and register please check this article
Secure login and registration with password hashing in Codeigniter

function userLogin($data = FALSE) {
        if ($data == FALSE) {
            $data = array(
                $user_email = $this->input->post('username'),
                $password = md5($this->input->post('password'))
        $this->db->where('email', $data[0]);
        $this->db->where('pwd', $data[1]);
        $this->db->where('activation_code is NULL', NULL);
        $this->db->where('status', 'A');
        $this->db->where('activation_date is not NULL', NULL);
        $query = $this->db->get('users', 1);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $rows) {
 //here we will set the session value once user get logged in.
                $newdata = array(
                    'user_id' => $rows->id,
                    'user_name' => $rows->name,
                    'user_image' => $rows->user_image,
                    'user_email' => $rows->email,
                    'logged_in' => TRUE,  
                    'user_type' => $rows->user_type,
                    'status' => $rows->status,
                    'acc_creation_date' => $rows->creation_date,
                    'acc_activation_date' => $rows->activation_date
            return true;
        } else {
            return false;

Now once user get logged in you just check for the session value in the constructor. Because every time when we run a function inside the controller constructor execute first then your function run.Let see how to check:

 if (!defined('BASEPATH'))
 exit('No direct script access allowed');
 class User extends CI_Controller {
 public function __construct() {
 //Here we will check for the session value if it is true then the function will run else it will redirect to login page
 if ($this->session->userdata('logged_in') == FALSE) {
 redirect(site_url('your login page'));
 } else {
   // if there is session value
   redirect(site_url('User/your function name');

Now what will happen each time when the user will run the User.php controller file at the very beginning it will check for the session value. and after checking the session it will take another action.You can skip the else part here.When your controller run and it will find the session value it will automatically go to the called function.
I hope this article Session handling in Codeigniter will help you.

How to Session handling in Codeigniter

I googled everywhere and checked for Session handling in Codeigniter only once for each controller but did not found any easy solution

Posted on 08-01-2016 


To comment you must be logged in members.

Files with category

File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd