About the PHP5 Session lifecycle

  • 2020-03-31 20:24:34
  • OfStack

It is determined by the Session ID, what is the Session ID, is the file name of the Session file, the Session ID is randomly generated, so it can guarantee the uniqueness and randomness, to ensure the security of the Session. Generally, if the lifetime of the Session is not set, the Session ID is stored in memory, which is automatically logged out after the browser is closed, and a new Session ID is registered after the page is requested again.

If the client does not disable the Cookie, the Cookie plays the role of storing the Session ID and Session lifetime when starting the Session Session. Let's manually set the lifetime of Session:

< ? PHP
Session_start ();
// keep it for one day
$lifeTime = 24 * 3600;
Setcookie (session_name(), session_id(), time() + $lifeTime, "/");
? >

In fact, PHP5 Session also provides a function session_set_cookie_params(); To set the lifetime of the PHP5 Session, the function must be called before the session_start() function is called:

< ? PHP
// keep it for one day
$lifeTime = 24 * 3600;
Session_set_cookie_params ($lifeTime);
Session_start ();
? >

Related articles: