Reputation: 800
what would be the best way to allow sessions to be used across all my controllers? i think there are a couple ways to do it:
1) define() session into global variables. I heard this is not best approach
2) set up a general MY_controller and extend every other controller to it. set up sessions there and that will make them available in all classes
3) something that has to do with adding &get_instance() to the __constructor() function that comes on the top of every controller
what would be the best way of setting sessions across all controllers?
i wasnt able to use any of these methods. any ideas would be helpful. Thanks
BTW, the problem was that codeigniter doesn't like simultaneous ajax requests. They interfere with each other and session data gets erased.
Upvotes: 1
Views: 10598
Reputation: 4006
Maybe you have to set some config variables in application/config/config.php
File
$config['cookie_domain']
and $config['cookie_path']
seems to be causing this problem. If so then just unset their value to "" and give it a try.
Upvotes: 2
Reputation: 11
Try setting the $config['cookie_secure']
to FALSE
when in development mode
Upvotes: 1
Reputation: 800
To anyone who is having trouble with codeigniter sessions, dont bother. They are done through cookies, and those have a lot of bugs if you have lots of ajax on your website. switch to native sessions.
Upvotes: 0
Reputation: 515
Once you set a session:
$this->session->set_userdata('name', 'value');
You can call it anywhere:
$this->session->userdata('name');
However, for session to work you need to initialize the Session class first manually! This can be done by calling this function:
$this->load->library('session');
Or you can let CodeIgniter to auto-load it:
application/config/autoload.php
$autoload['libraries'] = array('session');
It's a best practice to set a session in the controller.
I don't see any complications in here.
Upvotes: 5