CodeIgniter - Unable to access session in helper
Have you tried
Filename: application/helpers/Site_helper.php "First letter must all ways be upper case"
if ( ! function_exists('checkLogin')){ protected $CI; function checkLogin(){ $this->CI =& get_instance(); $this->CI->load->library('session'); // This only returns the id does not set it. return $this->CI->session->userdata('id'); }}
http://www.codeigniter.com/user_guide/general/ancillary_classes.html
Then on the controller
public function somefunction(){ $this->load->helper('site'); // Test only echo checkLogin();}
Make sure you have set your session save path I use the cache folder. This is the way I have set up mine. With folder permission 0700
$config['sess_driver'] = 'files';$config['sess_cookie_name'] = 'ci_session';$config['sess_expiration'] = 7200;$config['sess_save_path'] = APPPATH . 'cache/session/';$config['sess_match_ip'] = TRUE;$config['sess_time_to_update'] = 300;$config['sess_regenerate_destroy'] = TRUE;
$ci->session->user['id'];//replace with$ci->session->userdata("session_variable_name");
If it doesn't work please follow belowPlease check with your PHP version,I have also faced the same issue and then I got to know that I was building the application in Codeigniter 2 and PHP 5.6 and I check with Codeigniter 2.0 and PHP 7.3 version I got this Issue. So I tried with PHP 5.6 version and it worked.
Maybe you might have forgotten to load the session library
function checkLogin(){ $ci = &get_instance(); //load the session library $ci->load->library('session'); $session = $ci->session->user['id']; //this line throws errow }