Is possible to keep session even after the browser is closed?
Use session_set_cookie_parameters()
to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetime
to non-zero.
Can I make a session alive after when user closes his browser?
In order to make the session persist after closing the browser you need to set an expiry time for the session cookie. A cookie without an expiry time is deleted when the browser is closed, and is normally referred to as a session cookie (which is not the same thing as a PHP session - just related).
(side note: if your browser is configured to "save open tabs" at exit, then the session cookies may be saved by the browser even though they should be deleted)
So you could just set session.cookie_lifetime to a large value. But that doesn't stop the session data stored on your server from being deleted - to keep the data for longer you need to up the value for session.gc_maxlifetime.
BUT THIS IS THE WRONG WAY TO FIX THE PROBLEM
There are security and capacity implications to implementing such persistent sessions - you should certainly NEVER implement this as default behaviour - only where the user has explicitly given their consent.
Using a "Remember me" cookie as a sort of lightweight session system is the best practice solution. Give it a random value (suggest you use a reasonably reliable source of random numbers, e.g. base64_encode(openssl_random_pseudo_bytes(64))
and a name which does not conflict with other cookies, and store it along with the data you really want to persist across the actual sessions (e.g. authenticaticated username).
Maintaining the session even after the browser is closed
There is a really good tutorial on howto save session contents to a database.
The only thing needed is to refill a new session with the saved data - there you go.
Related Topics
Posting Form Fields With Same Name Attribute
How to Return Integer and Numeric Columns from MySQL as Integers and Numerics in PHP
How to Loop Through a MySQL Result Set
"[Notice] Child Pid Xxxx Exit Signal Segmentation Fault (11)" in Apache Error.Log
How to Replace MySQL Functions With Pdo
Making Sure PHP Substr Finishes on a Word Not a Character
Required Parameter $Xxx Follows Optional Parameter $Yyy
Send Json Data from JavaScript to PHP
How to Remove the Querystring and Get Only the Url
How to Overwrite a Function in PHP
Save Image from Url With Curl PHP
Woocommerce: Which Hook to Replace Deprecated "Woocommerce_Add_Order_Item_Meta"