Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

09

Jul

How to use session in Joomla

How to use session in Joomla

Posted On : July 9, 2013

| No Comment

Using Session In Joomla

Session plays an important role in web application. Session storage is used to store the user data over certain period of time. Session information is destroyed when user is inactive for certain period of time. Session information is stored on web server using session identifier (session ID) generated when the first request (sometimes authenticated) from the end user running web browser is received.

Global Setting for Session in Joomla

Global setting for session can be found in Site->Global Configuration->System.

Storage of Session in Joomla

Session information is stored in Database under the table “#__session” with unique session_id generated for each user

JSession Class for Session Management

Joomla has defined a class for managing HTTP session.This class is based on standard php session handling mechanism.It provides more advanced features such as expire timeouts.

Getting a session object

Session object cab be get by the getSession() method of JFactory class.

Usage:

1
$session =&JFactory::getSession();

Getting Session ID

Session ID can be get from getId() method of JSession class. You can also see the session ID in
The database in table “#__session” using phpMyAdmin.

Usage:

1
$session->getId();

Counting the Session usage

Session usage can be get from get() method of JSession class. Counter is incremented each time

When the request is sent to web server.

Usage:

1
$session->get('session.counter');

Checking if Session is currently created

Check whether the session is currently created by isNew() method of JSession class.

Usage:

1
2
if($session->isNew())
{  echo "NEW";  }  else {echo "OLD";}

Storing Session Data

Session data can be stored using set method of JSession class.

Usage:

1
$session->set('place','Kotdwara');

The above statement will store ‘Kotdwara’ in session variable ‘place’.We can also store array by following way.

Getting Session Data

Session data can be get using get() method of JSession class.

Usage:

1
$session->get('place','PLACE');

Where ‘PLACE’ is the default value. The above statement will get the value of session variable ‘place’.We can also get array by following way.

1
$mygetarray=$session->get('myarray');

Getting Session Name

Session Name can be get using getName() method of JSession class.

Usage:

1
$session->getName();

Getting Session State

Session State can be get using getState() method of JSession class. It represents the internal state of the session. getState() method will return any of the following states
1)active
2)expired
3)destroyed
4)error

Usage:

1
$session->getState();

Getting Session expiration time

Session expiration time (in sec can be get using getExpire() method of JSession class.
Usage:

1
$session->getExpire();

Clear Session Data

To Unset data from the session store use clear() method of JSession class.

Usage:

1
$session->clear('place');

Where ‘place’ is the name of the session variable to unset.

Getting a session token

To get session token use getToken() method of JSession class. Tokens are used to secure forms against the spamming attacks. Once a token has been generated the system will check the post request to see if it is present, if not it will invalidate the session.

Usage:

1
$token = $session->getToken();

Determine if Token Exist

To determine if token exist in the session use hasToken() method of JSession class.Method returns true if the particular token exist in the session, if not session is set to ‘expired’.

Usage:

$session->hasToken($token);
Where $token is the token to check in the session.

Getting available session handlers

To get the list of available session handlers use getStores() method of JSession class.

Usage:

1
$stores = $session->getStores();

Testing the Joomla session

Place the following code in your current template’s index.php file after the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"<!--?php defined('_JEXEC')  or die('Resticted access'); ?-->"
$session =&amp;JFactory::getSession();
$orders = array();
$orders[0] = "FIRST" ;
$orders[1] = "SECOND" ;
$orders[2] = "THIRD" ;
$orders[3] = "FORTH" ;
$orders[4] = "FIFTH";
echo "ID: ".$session-&gt;getId();
$session-&gt;set('place','Kotdwara');
$session-&gt;set('myarray',$orders);
echo "
Count: ".$session-&gt;get('session.counter');
If($session-&gt;isNew())
{echo "
NEW ";}
else{echo "
OLD ";}
if($session-&gt;has('place12'))
{echo "
HAS ";}
else{echo "
NOHAS ";}
$mygetarray = $session-&gt;get('myarray');
echo "
Place: ".$session-&gt;get('place','PLACE');
echo "
Expire: ".$session-&gt;getExpire();
echo "
State: ".$session-&gt;getState();
echo "
Session Name: ".$session-&gt;getName();
echo "
ONE: ".$mygetarray[1];

You will see the following output in your template :

Every time you refresh the page, count will be incremented. When you close the browser and reopen the site, count will be 1 and NEW will be displayed.

  • Tags:

Comment