PHP 8.4.0 RC4 available for testing

SessionUpdateTimestampHandlerInterface::updateTimestamp

(PHP 7, PHP 8)

SessionUpdateTimestampHandlerInterface::updateTimestampUpdate timestamp

Descripción

public SessionUpdateTimestampHandlerInterface::updateTimestamp(string $id, string $data): bool

Updates the last modification timestamp of the session. This function is automatically executed when a session is updated.

Parámetros

id

The session ID.

data

The session data.

Valores devueltos

Returns true if the timestamp was updated, false otherwise. Note that this value is returned internally to PHP for processing.

add a note

User Contributed Notes 4 notes

up
6
tuncdan dot ozdemir dot peng at gmail dot com
1 year ago
I am not sure why the data is needed for this method if it only updated the timestamp only. Otherwise wouldn't his be the same the 'write' method? I think it is confusing and the manual unfortunately doesn't give much information about the whole session mechanics.
up
5
ohcc at 163 dot com
4 years ago
When session.lazy_write is enabled, which is the default behaviour, session data will NOT be UPDATED if it remains unchanged, in this way the WRITE method of the session handler will not be called at all.

If your session handler storages session data into files, UpdateTimestamp is used to update the "last modified time" of the session file, if your session handler storages session data into a database, UpdateTimestamp is used to update the table field that storages the last modified time of the session registry.
up
2
ohcc at 163 dot com
4 years ago
'validateId' is called after 'open' and before 'read' to validate the session id provided by the client, as 'open' -> 'validateId' -> 'read' -> 'write' -> 'close' are called in sequence.

If 'validateId' returns false, a new session id will be generated, the session cookie will also be updated afterwards.
up
0
polygon dot co dot in at gmail dot com
16 days ago
Example of using $sessionData available in updateTimestamp function.

This is useful when we try to use cookie as a container to save session data.
In cookie we need to maintain the access timestamp on similar lines of access time of file.

Here we have session.serialize_handler as php_serialize to use serialize() and unserialize() for session encoded data.

<?php
public function updateTimestamp($sessionId, $sessionData)
{
$sessionDataArr = unserialize($sessionData);
$sessionDataArr['_TS_'] = $this->currentTimestamp;
$sessionData = serialize($sessionDataArr);

$cookieData = $this->encryptData($sessionData);
if (
strlen($cookieData) > 4096) {
ob_end_clean();
die(
'Session data length exceeds max 4 kilobytes (KB) supported per Cookie');
}

return
setcookie(
$name = $this->sessionDataName,
$value = $cookieData,
$options = [
'expires' => 0,
'path' => '/',
'domain' => '',
'secure' => ((strpos($_SERVER['HTTP_HOST'], 'localhost') === false) ? true : false),
'httponly' => true,
'samesite' => 'Lax'
]
);
}
?>
To Top