session service definition
//create new session instance
$daemon = new \Phalcon\Session\Adapter\Libmemcached(
['servers' => [['host' => $config->_daemon->socket, 'port' => $config->_daemon->pipe, 'weight' => $config->_daemon->weight]],
'client' => [
Memcached::OPT_HASH => Memcached::HASH_MD5,
Memcached::OPT_PREFIX_KEY => 'web.', //this is the KEY DOMAIN!
Memcached::OPT_TCP_NODELAY => 1,
Memcached::OPT_NO_BLOCK => 1,
Memcached::OPT_CONNECT_TIMEOUT => 500
],
'lifetime' => 900, //default lifetime of a session
'prefix' => 'session_', //actual prefix of the keys
]);
//NOTE on lifetime: the actual value sent may either be Unix time (number of seconds since January 1, 1970, as an integer),
//or a number of seconds starting from current time.
//In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days);
//Set session cookie name
$daemon->setName('MySweetAppCookie');
//Start session - Set Cookie HTTP header
$daemon->start();
//return session instance
return $daemon;
ControllerBase
onConstruct()
method implements this check:
protected function isUserConnected() {
return $this->session->has('userLoggedIn') ? true : false;
}
Note: this shold not be necessary as the shared Session service should trigger this by default, if it doesn't, call to method has()
will surely do the job.
End result
if the user is active on the site, i.e. browsing pages etc. the has()
method will contact Memcached daemon and update lifetime each time. This way, user will always have 15 min session lifetime before expiration. If he/she leaves the computer, session will expire.