How to keep a session open after closing the browser in PHP?

I followed the answer from here, but it didn’t work for me:
Is it possible to continue the session even after closing the browser?

Here is the code I tried:

$session_life = 2592000; // 30 days
session_set_cookie_params($session_life);
session_name('my_cart');
session_start();
// update the session_life
setcookie(session_name(),session_id(),time()+$session_life);

The problem is that every time the browser closes, I still get a new session_id, not the old one.

I use a database to store items in 'my_cart', and session_id is just used to identify the user to show them their own cart.

What is the best way to keep users for 30 days?


This is the code I ended up in:

$cart_name = "my_cart";
$cart_life = 2592000; // 30 days
session_start();
if (isset($_COOKIE[$cart_name])) {
    session_id($_COOKIE[$cart_name]);
}
setcookie($cart_name, session_id(), time()+$cart_life);
+5
source share
1 answer

, ( 30- , )

, cookie ( , ), . , , (, , ), .

,

, , , . :

update basket set expiryDateTime=date_add(now(),interval 7 day) where basketID=[xyz]

, (20:00), MySQL

+3

All Articles