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).
how to keep the session alive after browser closed in codeigniter?
first step is create a token field in databse for that userid and when we create session for the first time for the user set cookie token an dupdate in databse. and then everytime on login check that user cookie from browser and check with the database token matched and start the session for the user.
Please check wiht my below code
$values = array(
'user_id' => $row->id,
'logged_in' => TRUE,
'role' => $row->role_id,
'email' =>$row->email
);
$this->session->set_userdata($values);
$cookie = array(
'name' => 'token',
'value' => generateRandomString() ,
'expire' => '1209600',
'domain' => domain() ,
'path' => '/'
);
set_cookie($cookie);
$update = array(
'token' => $cookie['value'],
);
$this->db->where('id', $row->id);
$this->db->update('user', $update);
if ($CI->session->userdata('logged_in')==1 && $CI->session->userdata('user_id')) {
$query = $CI->db->query("select * from user where token= '".$token."' and role_id=2");
$result = $query->row();
if($result->token==$token){
$values = array(
'user_id' => $result->id,
'logged_in' => TRUE,
'role' => $result->role_id,
'email' =>$result->email
);
$CI->session->set_userdata($values);
Related Topics
In Magento 1.9.0.0 How to Enable Template Path Hint
Image Upload from iPhone Strips Exif Data
How to Add 5 Minutes to Current Datetime on PHP < 5.3
Storing Leading Zeros of Integers in MySQL Database as Integer
Base64 Over Http Post Losing Data (Objective-C)
How to Avoid Request Entity Too Large 413 Error
How Does PHP Max_Execution_Time Work
PHP Dynamically Create CSV: Skip the First Line of a CSV File
How to Remove Exif from a Jpg Without Losing Image Quality
PHP Curl: Curlopt_Connecttimeout VS Curlopt_Timeout
Selenium2 Firefox: Use the Default Profile
PHP MySQL Pagination with Random Ordering
Php: Move_Uploaded_File() Failed to Open Stream: No Such File or Directory
Why Doesn't PHP Permit Private Const
Setting Default Values (Conditional Assignment)
What's the Difference Between Post and Raw Post in PHP at All