Back to Top

How to use Session in Joomla

How to use Session in Joomla

Joomla is a powerful content management system for many reasons. Here is one quick article about how to use a custom session variable in Joomla.Today I have setup custom session variable in Joomla So I thought it would be good to share how to get and set session variables in Joomla.

Joomla uses own session management so if you use PHP session with Joomla there is no guarantee that it works properly. To get values use Joomla session management with using a the session handling code.Joomla session uses a class JSession with which we can work with sessions in the Joomla framework.

How to set a session variable:

The below code gets the current session and sets the value of the session variable ‘Sess_var’ to ‘yes’.

To assign the value in Joomla using the session, $session as a reference to an object for the current session.Using $session, you can set the session and in that first is the name of the variable and the second parameter is the value of the variable.

How to get a session variable:

After setting session, To retrieve session variable,

Here, I have used JFactory reference variable with function getSession and assign it to variable $session.Next, You can use that variable to retrieve the specific session variable.

How to unset a session variable:

If you get and set the session in Joomla, You might want to clear the session or remove the session variable. Use below code to unset session variable after using it,

In clear function, You just need to pass the name of the session variable. You would like to further read more articles are how to hide the menu from logged in User in Joomla and Sorting in Joomla.

Hopefully this article will help someone. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates. Do you have any question about Joomla Session or any issue in using any variable? Let me know in comment section.

Comments (3)

  1. You have showed great perseverance behind the blog. It’s been enriched since the beginning. I love to share to with my friends. Carry on.

  2. Session in Joomla | Creative Dev Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again very soon!

  3. Hi, I can’t clear my session variable. When I do that, I cant’t display it. My code:

    if ($msg = $session->get(‘message’)) :
    display message
    endif;

    $session->clear(‘message’)

    I don’t know what I’m doing wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to set a Cookie in PHP

Posted on 12 years ago

Bhumi

Overview of ECMAScript 6 new Features

Posted on 6 years ago

Bhumi

The Readers’ Poll – August 2012

Posted on 12 years ago

Bhumi