API16

API16:JSessionStorageDatabase/write

From Joomla! Documentation

Description

Write session data to the SessionHandler backend.



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();
}



Examples

Code Examples