Jump to content
  • Advertisement
Sign in to follow this  
Brainx7

Keeping session alive after closing browser using php

This topic is 1345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have a form that uses session variables , and so I would like that when I close the browser and reopen it, the session is still alive
 

// This is suppose to keep the session even if I close the browser but is not working
//It works locally but not on the server, How can I fix this?
ini_set('session.cookie_lifetime', 60 * 60 * 24 * 365);
ini_set('session.gc-maxlifetime', 60 * 60 * 24 * 365);
  session_start();
 
  function input_val($key, $remember = true) {         //use input_val('nameofinputfield')as value to be able to store in session
   $value='';
    if(isset($_REQUEST[$key])) {
       $value = $_REQUEST[$key];
       //Store value in session if remember = true
       if($remember) {
         $_SESSION[$key] = $value;
       }
 
       
    
       return $value;
    }else{
      //Return session data
      return isset($_SESSION[$key]) ? $_SESSION[$key] : $value;
    }
}
Edited by Brainx7

Share this post


Link to post
Share on other sites
Advertisement

Apparently nobody is allowed to have persistant session cookies on the server for security reasons, is there any other way to do this?

Share this post


Link to post
Share on other sites

What isn't working?

my PHP is a bit rusty, but you should be able to store and retrieve values from the $_SESSION variable. If you only want persistent form data, I would look into using localstorage. Doing it on the client end this way will allow you to save data without requiring any form submissions or ajax calls to the server. Just don't store any sensitive information such as passwords or credit cards.

Share this post


Link to post
Share on other sites

Session on server does not expire when client closes browser, the client session expires. If you want client to call for his session even after reopening browser a day later, you need to set his session cookie header with explicit expire time -what you are doing. But of course, the session on server will be expired (out of reasonable resoures usage, after empty 60 minutes in best case), so you will have to restore the sessioned data that you should make persistent (from sql database for example). Every session has a session ID so you can store them and identify stored data with a client requesting a session that is already server free, or identify the client with account, or annything.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!