Actions

API16

JSessionStorageDatabase/write

From Joomla! Documentation

< API16:JSessionStorageDatabase
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Write session data to the SessionHandler backend.

[Edit Descripton]

Description:JSessionStorageDatabase/write

Syntax

write($id, $data)
Parameter Name Default Value Description
$id The session identifier.
$data The session data.

Returns

boolean True on success, false otherwise.

Defined in

libraries/joomla/session/storage/database.php

Importing

jimport( 'joomla.session.storage.database' );

Source Body

public function write($id, $data)
{
        // Get the database connection object and verify its connected.
        $db = &JFactory::getDbo();
        if (!$db->connected()) {
                return false;
        }
 
        // Get the session data from the database table.
        $db->setQuery(
                'SELECT `session_id`' .
                ' FROM `#__session`' .
                ' WHERE `session_id` = '.$db->quote($id)
        );
        $exists = $db->loadResult();
 
        // If the session exists we just need to update the data field.
        if ($exists) {
                $db->setQuery(
                        'UPDATE `#__session`' .
                        ' SET `data` = '.$db->quote($data).',' .
                        '       `time` = '.(int) time() .
                        ' WHERE `session_id` = '.$db->quote($id)
                );
        }
        // If the session does not exist, we need to insert the session.
        else {
                $db->setQuery(
                        'INSERT INTO `#__session` (`session_id`, `data`, `time`)' .
                        ' VALUES ('.$db->quote($id).', '.$db->quote($data).', '.(int) time().')'
                );
        }
 
        // Write the session data to the database.
        return (boolean) $db->query();
}

[Edit See Also] SeeAlso:JSessionStorageDatabase/write

Examples

<CodeExamplesForm />