API16

JSession/set

From Joomla! Documentation

< 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[edit]

Set data into the session store.



Syntax[edit]

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[edit]

mixed Old value of a variable.

Defined in[edit]

libraries/joomla/session/session.php

Importing[edit]

jimport( 'joomla.session.session' );

Source Body[edit]

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[edit]

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"));