This is really driving me mad. I have used this without any issues in the past.
So I have a Codeigniter installation, in which I have autoloaded session. I am also also autoloading a helper named "site" (site_helper.php).
Now in site_helper I do the following:
function checkLogin(){
$ci = &get_instance();
$session = $ci->session->user['id']; //this line throws errow
}
checkLogin();
What I get as result is:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Welcome::$session
Filename: helpers/site_helper.php
Line Number: 15
If you are wondering what "welcome" is, its my default controller. This has worked well in other sites I have worked on. I am at a total loss what the issue is here.
Any help is appreciated!
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;
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 }
Make Sure you have auto load session libaray in autoload.php file
Autoload.php : $autoload['libraries'] = array('session');
For Accessing the session variable :
$CI = & get_instance();
//or you can load library in the site helper also
$CI->load->library('email');
$data = $CI->session->all_userdata();
Add
print_r($data)
. You will get useful results.