Making a session persistent

Making a session persistent – This example illustrates how to make a session persist over multiple browser sessions using a so called "session cookie".

Explanation

When you create a session it needs to be referenced using a sessionID. This sessionID is generally saved either into a cookie or passed along in the URI.

To remember the sessionID across browser sessions, the cookie method is favoured.

To extend the lifetime of this cookie (the standard is that it's only valid until the browser is closed) we use PHP's session_set_cookie_params().

Example

Use of session_set_cookie_params

<?php
require_once 'HTTP/Session2.php';

/**
 * set cookie params
 */
session_set_cookie_params(60*60*24// expire in 24 hours
    
'/',                            // path
    
'.example.org',                 // domain
    
false,                          // "secure only"
    
true);                          // only over http

HTTP_Session2::useCookies(true);

HTTP_Session2::start('s');
HTTP_Session2::setExpire(60*60*24); // set expire in 24 hours
HTTP_Session2::setIdle(60*60);      // set idle to 1 hour

if (HTTP_Session2::isExpired()) {
    
HTTP_Session2::destroy();
}

if (
HTTP_Session2::isIdle()) {
    
HTTP_Session2::destroy();
}

HTTP_Session2::updateIdle();
?>
How to store session data in a database with HTTP_Session2 (Previous) HTTP_Upload (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.