API16

Difference between revisions of "JSession/set"

From Joomla! Documentation

< API16:JSession
(New page: ===Description=== Set data into the session store. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki> ...)
 
(→‎Examples: Port over example from framework page)
Line 67: Line 67:
  
 
===Examples===
 
===Examples===
<CodeExamplesForm />
+
May be obvious to some but had me going:<br/>
 +
Feel free to correct me on this but if you want to set a custom object you need to serialize it:<br/>
 +
 
 +
<source lang="php">
 +
$session =& JFactory::getSession();
 +
$session->set("MyKeyName", serialize($MyObject));
 +
$MyObject = unserialize($session->get("MyKeyName"));
 +
</source>
 
<dpl>
 
<dpl>
 
  noresultsheader=\n
 
  noresultsheader=\n

Revision as of 07:27, 14 May 2013

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.

[Edit Descripton]

Template:Description:JSession/set

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

[Edit See Also] Template:SeeAlso:JSession/set

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