API16

JSession/set

< API16:JSession

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description

Set data into the session store.



Syntax

set($name, $value=null, $namespace= 'default')
Parameter Name Default Value Description
$name Name of a variable.
$value null Value of a variable.
$namespace 'default' Namespace to use, default to 'default'.

Returns

mixed Old value of a variable.

Defined in

libraries/joomla/session/session.php

Importing

jimport( 'joomla.session.session' );

Source Body

public function set($name, $value = null, $namespace = 'default')
{
        $namespace = '__'.$namespace; //add prefix to namespace to avoid collisions

        if ($this->_state !== 'active') {
                // @TODO :: generated error here
                return null;
        }

        $old = isset($_SESSION[$namespace][$name]) ?  $_SESSION[$namespace][$name] : null;

        if (null === $value) {
                unset($_SESSION[$namespace][$name]);
        } else {
                $_SESSION[$namespace][$name] = $value;
        }

        return $old;
}



Examples

May be obvious to some but had me going:
Feel free to correct me on this but if you want to set a custom object you need to serialize it:

$session =& JFactory::getSession();
$session->set("MyKeyName", serialize($MyObject));
$MyObject = unserialize($session->get("MyKeyName"));


Advertisement