Take the 2-minute tour ×

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 __contructor() 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 helpfull. Thanks

BTW, the problem was that codeigniter doesnt like simultaneous ajax requests. They interfere with each other and session data gets erased.

share|improve this question
a $_SESSION is a super global, its available within all your controllers. Can you explain what you mean by session if not $_SESSION? –  Loz Cherone ツ May 4 '12 at 1:57
not global in codeigniter –  Semur Nabiev May 4 '12 at 2:25

4 Answers 4

up vote 1 down vote accepted

Maybe you have set (in application/config/config.php) $config['cookie_domain'], $config['cookie_path'] to something which is causing this problem. If so then just unset them to "" and give it a try.

share|improve this answer
that was it. had to be simple –  Semur Nabiev May 4 '12 at 18:43

Once you set a session:

$this->session->set_userdata('name', 'value');

You can call it anywhere:


However, for session to work you need to initialize the Session class first manually! This can be done by calling this function:


Or you can let CodeIgniter to auto-load it:



It's a best practice to set a session in the controller.

I don't see any complications in here.

share|improve this answer
thing is, codeigniter doesnt use sessions, so there is no global array where i can access them. once i move to a different controller i lose them –  Semur Nabiev May 4 '12 at 2:23
"once i move to a different controller i lose them" - Are you sure? Elaborate more, I've been using CodeIgniter for a while and I don't encounter what you just said. –  deex May 4 '12 at 2:26
ok, so i do set_userdata() and i can echo it in the controller from which i called the model that did set_userdata(). but when i try to echo these variables in another controller they are just not there. has that happened to you? –  Semur Nabiev May 4 '12 at 2:34
see my edited post. –  deex May 4 '12 at 2:39
done that, but still not working. but thanks for your time –  Semur Nabiev May 4 '12 at 2:53

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.

share|improve this answer

Try setting the $config['cookie_secure'] to FALSE when in development mode

share|improve this answer

Your Answer


