XMF Cookbook
  • Introduction
  • XMF Cookbook
  • Basic Ingredients
    • Namespaces
    • Autoloading
    • Debugging
    • Forward Compatibility
  • Recipes
    • Introducing Module Helpers
    • Using the Permission Helper
      • Checking Permissions
      • Managing Item Permissions
    • Using the Session Helper
    • Using JSON Web Tokens
    • Altering Database Tables
    • Migrating a Module's Database
    • Loading Initial Data
    • Module Admin Pages
      • Hide and Seek with Icons
      • Standard Admin Pages
    • Manage Metadata
    • Highlighting Content
  • Reference
    • Assert
      • Assertions
    • Database
      • Migrate
      • TableLoad
      • Tables
        • Getting Started
        • Table Operations
        • Working with Columns
        • Working with Indexes
        • Changing Table Data
        • Interacting with the Work Queue
        • Error Info and Debugging
    • Debug
    • FilterInput
    • Highlighter
    • IPAddress
    • Jwt
      • JsonWebToken
      • KeyFactory
      • TokenFactory
      • TokenReader
    • Key
      • ArrayStorage
      • Basic
      • FileStorage
      • KeyAbstract
      • StorageInterface
    • Language
    • Metagen
      • Extracting Data
      • Applying Data
    • Module
      • Admin
      • Helper
      • Helper
        • AbstractHelper
        • Cache
        • GenericHelper
        • Permission
        • Session
    • ProxyCheck
    • Random
    • Request
    • StopWords
    • Uuid
    • Yaml
  • Credits
  • License:
  • Table of Content
Powered by GitBook
On this page
  1. Recipes

Using the Session Helper

PreviousManaging Item PermissionsNextUsing JSON Web Tokens

Last updated 6 years ago

Saving context information is a common need. Often times programmers jump straight to the HTTP cookie to save context between browser requests. Often times, this is overkill. XOOPS maintains a very capable session mechanism. If your data only needs to last until the user logs out, using the session instead of a cookie may be more efficient. So if you need to remember something like the last set of criteria a user entered on a certain page of your site, consider the session before the cookie.

The class does a number of favors for you. First, it is module aware. It will use your current module name as a prefix to your session key. Using this approach, we can share the session space between the core system and other modules without having conflicts in our names. Also, your value is always serialized before it is saved in the session, so you can store anything without worrying about the type, or converting it back and forth. (If you do store an object, make sure the class for that object is available before you get() it.)

Here is how you can save data to the session using XMF. For this example, we assume your data is in the variable $value, and your key is 'context'. Value can be pretty much anything, a single value, an array, even an object.

Save and Retrieve Context

The fist step is always to get a session helper.

$sessionHelper = new \Xmf\Module\Helper\Session();

Save the data like this:

$sessionHelper->set('context', $value);

Retrieving your value is just as easy:

$value = $sessionHelper->get('context');

If you are finished with the value you saved and no longer need it, you can delete it:

$sessionHelper->del('context');

All session data will disappear once the user logs out, or the session expires from inactivity.

Xmf\Module\Helper\Session