Codeigniter Session Issue

Posted on January 13, 2016 by Suresh Kamrushi in PHP

Due to Codeigniter session issue i need to go away from native session class of it and look for alternative. Initially I try to debug it, but not luck. So here is the code how I used PHP session instead codeginiter.

There may be two approaches to achieve this either create your own library or override the existing one in both case below code will be same except class name.

if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Session {
 private $userdata;

 public function __construct() {
   if(!isset($_SESSION)) { 
     session_start(); 
   }
 }

 function userdata($key) {
   return isset($_SESSION[$key]) ? $_SESSION[$key] : "";
 }

 function set_userdata($newdata = array(), $newval = '') {
     if (is_string($newdata)) {
       $newdata = array($newdata => $newval);
     }
     if (count($newdata) > 0) {
       foreach ($newdata as $key => $val) {
          $this->userdata[$key] = $val;
        }
     }
      $this->sess_write();
    }
    
    public function sess_write() {
        foreach ($this->userdata as $key => $val) {
            $_SESSION[$key] = $val;
        }
    }
    function set_flashdata($newdata = array(), $newval = '')     {
        $this->set_userdata($newdata,$newval);
    }

    public function flashdata($key) {
        return isset($_SESSION[$key]) ? $_SESSION[$key] : "";
    }
    
    function sess_destroy() {
        unset($_SESSION);
        session_unset();
    }
}

if (!defined('BASEPATH'))
 exit('No direct script access allowed');
class CI_Session {

 private $userdata;

 public function __construct() {
 if(!isset($_SESSION)) { 
 session_start(); 
 }
 }

 function userdata($key) {
 return isset($_SESSION[$key]) ? $_SESSION[$key] : "";
 }

 function set_userdata($newdata = array(), $newval = '') {
 if (is_string($newdata)) {
 $newdata = array($newdata => $newval);
 }
  Comments or questions are welcome  
Tagged as ,