Actions

API16

JSession/set

From Joomla! Documentation

< API16:JSession
Revision as of 07:27, 14 May 2013 by Wilsonge (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

Set data into the session store.

[Edit Descripton]

Description:JSession/set

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

[Edit See Also] SeeAlso:JSession/set

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