Welcome Guest, Not a member yet? Register   Sign In
Session data lost after redirect randomly (CI4.1.5)
#1
Photo 
(This post was last modified: 08-23-2022, 03:58 AM by pJenkiss.)

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]
Reply
#2

I would upgrade to the newest version of CodeIgniter 4.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

did you just replace session data, not merge with previous data
Reply
#4

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 09-27-2022, 04:20 AM by pJenkiss.)

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)
Reply
#6

Did you try updating to CodeIgniter 4.2.6 ?

There maybe an update to the Sessions.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(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..
Reply
#8

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-l...l-example/
Thanks!
Reply
#9

Hi pJenkiss / tipitjafar

Could you solve this issue? If so, how?
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB