[EDIT - I have posted the answer to my question below]

EE 5.2.4

I'm having issues trying to use $_SESSION in EE.

What I would like to do is save an Amazon cart ID in the $_SESSION. Working in regular PHP I'm familiar with this: $_SESSION['cart_id'] = 1234. However when I try to do this in EE I get errors and/or nothing happens.

I thought that maybe what I'm looking for is the cache-access functionality:

So in my class I have this ee()->session->set_cache('amz_cart', 'id', $value);, and in my view I'm trying to see what returns from this ee()->session->cache('amz_cart', 'id');.

Nothing returns with this. Can anyone point me in the right direction?


  • btw for v 5 you need to use differently - ee()->session->cache['amz_cart']['id'] = $value & ee()->session->cache['amz_cart']['id'] for return data. – Max Lazar Jul 11 '19 at 20:01
  • Can I get more information about this? None of the options I'm seeing are working the same way that $_SESSION does. I would simply use $_SESSION but when I do var_dump($_SESSION) I get Undefined variable: _SESSION. I need this variable to be global. It seems like it should be a simple answer and I'm not sure why $_SESSION isn't permitted. – Shayne Jul 11 '19 at 21:04

Well, you can use flashdata to achieve your requirement. For this please follow below syntax.

To set the Flashdata:-

ee()->session->set_flashdata('your_key', 'your_value');

To get the Flashdata:-

echo ee()->session->flashdata('your_key');


  • Thanks for the response Barach! I've been trying to work with this and have run into a few problems. The docs say flashdata only works when followed by a redirect. I'm using $.ajax to run a class method and in that method I have ee()->session->set_flashdata('amz_cart', 'test'); followed by ee()->functions->redirect(ee()->functions->fetch_current_uri());. This does not appear to persist, however, and I need it to be checkable across the site (if this session variable exists, show a cart in the header, for ex). – Shayne Jul 11 '19 at 15:56
  • I should add that if I echo the flashdata after running the redirect in the class method echo ee()->session->flashdata('amz_cart'); I get {"amz_cart":"test"}, but if I refresh the page I can't seem to retrieve the flashdata. – Shayne Jul 11 '19 at 15:59
  • session->cache it is also "flash" content. It is not like $_SESSION – Max Lazar Jul 11 '19 at 19:47

It appears as though I've answered my own question by trial and error.

In my class method I can set the value like this:

$_SESSION['amz_cart']['id'] = $value;

and in the template when I want to access that value I have to do this:

    echo $_SESSION['amz_cart']['id'];

I'm not sure why it works this way but it appears to be working for my needs for the time being.

I'm still interested in a more elegant or EE-based solution that will work the same way.

Thanks everybody!

  • 1
    it works exactly because you need to start sessions first. Two moments - better to use if ( session_id() == "" ) { session_start(); } And be careful if you do code which should work with load balancer and the user could be switch between servers because $_SESSION is not cross servers data – Max Lazar Jul 12 '19 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.