CodeIgniter Forums
Session data lost after redirect randomly (CI4.1.5) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Session data lost after redirect randomly (CI4.1.5) (/showthread.php?tid=82784)

Pages: 1 2 3


Session data lost after redirect randomly (CI4.1.5) - pJenkiss - 08-23-2022

Hello,
I wrote a post some time ago about another issue (https://forum.codeigniter.com/showthread.php?tid=80668), but I still facing various issues with lost session data from time to time. And I have no idea what I do wrong, because it happens randomly. I catched this situation in logs (see image below). The situation:
I add products to "cart" and save it to session. Then redirect to cart page and show cart details. However, from time to time the cart appears to be empty, even though it was added successfully. 
Here is my last line in "cart edit" controller function:
PHP Code:
$sessionData = ['cart' => $cart'orderId' => $order['id']];
$this->session->set($sessionData);
log_message('error''Debug: add to cart, session set: '.json_encode($_SESSION));
return 
redirect()->to('/cart'); 

This is the beginning of the code from "cart" controller function:
PHP Code:
log_message('error''Debug: cart init, session: '.json_encode($_SESSION)); 

In the image you can see two attempts to add new product to a cart, first time product was added and after redirect it stayed in the session, second time cart was not there after redirect. Sometimes I need to repeat the cycle 3, sometimes - 5 times in order session to keep data after redirect.
Any ideas at what to look at? 
I work on single tab, session expiration is set for 1 month. 
I work with CI for years, but since we moved to version 4 earlier this year session issues keep bugging me.. 
[Image: Screenshot-2022-08-23-134322.jpg]


RE: Session data lost after redirect randomly (CI4.1.5) - InsiteFX - 08-23-2022

I would upgrade to the newest version of CodeIgniter 4.


RE: Session data lost after redirect randomly (CI4.1.5) - ikesela - 08-24-2022

did you just replace session data, not merge with previous data


RE: Session data lost after redirect randomly (CI4.1.5) - InsiteFX - 08-25-2022

You could try this merging the arrays:

PHP Code:
$cartData $session->get();
$newData  = ['cart' => $cart'orderId' => $order['id']];

$sessionData array_merge($cartData$newData);

$this->session->set($sessionData);
log_message('error''Debug: add to cart, session set: '.json_encode($_SESSION));
return 
redirect()->to('/cart'); 


See if that works, I do not have the time to test it right now.


RE: Session data lost after redirect randomly (CI4.1.5) - pJenkiss - 09-27-2022

Nothing helped. There is something fundamentally wrong with sessions in CI. One essential thing that makes me think that way: When I refresh the same page (without login), every few seconds a new record is added to "ci_sessions" table. Which is incorrect comparing to documentation (and sessions in general):

When a page is loaded, the session class will check to see if a valid
session cookie is sent by the user’s browser. If a sessions cookie does
not exist (or if it doesn’t match one stored on the server or has
expired) a new session will be created and saved.

Note: not every refresh creates new record, it happens randomly. Sometimes it creates it every refresh, sometimes it's the same for minute or so (random time, random amount of refreshes)


RE: Session data lost after redirect randomly (CI4.1.5) - InsiteFX - 09-28-2022

Did you try updating to CodeIgniter 4.2.6 ?

There maybe an update to the Sessions.


RE: Session data lost after redirect randomly (CI4.1.5) - pJenkiss - 10-07-2022

(09-28-2022, 12:39 AM)InsiteFX Wrote: Did you try updating to CodeIgniter 4.2.6 ?

There maybe an update to the Sessions.

Yes, latest version, issues remain..


RE: Session data lost after redirect randomly (CI4.1.5) - tipitjafar - 10-17-2022

Hello people,
Here facing an even worst scenario. I cant simply pass session data from one request to another. $_SESSION array just erases on redirect or when a new page is loaded.
I've tryed every internet solution found, even intalled my app on another fresh server and nothing changes. Just can't make a login page to work... unvelievable.
I've edited session and cookie classes fordebuggin and couldn't find what's going on.
For the record, i've followed this super simple example just to not geting into any trouble, but instead...
Example: https://www.tutsmake.com/codeigniter-4-login-and-registration-tutorial-example/
Thanks!


RE: Session data lost after redirect randomly (CI4.1.5) - kabeza - 06-22-2023

Hi pJenkiss / tipitjafar

Could you solve this issue? If so, how?


RE: Session data lost after redirect randomly (CI4.1.5) - pJenkiss - 06-22-2023

(06-22-2023, 11:09 AM)kabeza Wrote: Hi pJenkiss / tipitjafar

Could you solve this issue? If so, how?

Unfortunatelly no. We're facing random logouts on the system.