Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Simple Sessions Class

, 9 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A very simple class for manipulating sessions in PHP.

Update: I had to move this project due to a plugin error.

Simple Sessions Class

This here class is a very simple one for manipulating sessions in PHP. In fact, it is so simple, that it makes it self explanatory, but nonetheless, I will take the time to document it.

First off, you might notice that the first line is:

<?php if(!defined('BASEPATH')) exit('No direct script access allowed'); ?>

I have been using the codeigniter framework for my web projects, and they do not use the traditional sessions within PHP, and while trying to use their sessions library, I have encountered a bit of problems that have not been fixed yet. I then decided to create my own library (class) to implement traditional sessions. In short, the way this class is currently implemented, it can be used in normal PHP projects or add it as a library for codeigniter. If you are going to use this library in normal php, then you can comment out the first line, or even delete it as needed.

Normal PHP: (assuming it is in your same folder)
Require this class at the top of your script

<?php require_once('Simple_sessions.php');?>

Codeigniter usage:

Copy this class to your applications/libraries folder. This can be autoloaded or loaded from the controller:

In the autoload.php file, you can add simple_sessions in the libraries index.

<?php
    $autoload['libraries'] = array('simple_sessions');
?>

In Controller:

<?php
    $this->load->library('simple_sessions');
?>

Adding Session variables

In order to add new session variables, you will need to use the function add_sess. This function expects an array parameter. This can be done with the following example:

Normal PHP:

<?php 
    $my_obj = new Simple_sessions();
    $data = array(
                'username'     => $username,
                'userid'     => $userid,
                'fullname'     => $fullname,
                'status'     => $status
            );
    $my_obj->add_sess($data);
?>

Codeigniter:

<?php
    $this->load->library('simple_sessions');
    $data = array(
                'username'     => $username,
                'userid'     => $userid,
                'fullname'     => $fullname,
                'status'     => $status
            );
    $this->simple_sessions->add_sess($data);
?>

Editing and Deleting a Session variable

If at any moment you need to edit or delete a session variable, then we use following functions respectively edit_sess($name) or del_sess($name). Examples:

Normal PHP:

<?php 
    // To edit fullname
    $my_obj->edit_sess('fullname', 'My New Name');
    // To delete status 
    $my_obj->del_sess('status');
?>

Codeigniter:

<?php 
    // To edit fullname
    $this->simple_sessions->edit_sess('fullname', 'My New Name');
    // To delete status
    $this->simple_sessions->del_sess('status');
?>

Retrieve Session ID

To be able to retrieve the session id, use the function get_sess_id(). Examples:

Normal PHP:

<?php
    $session_id = $my_obj->get_sess_id();
?>

Codeigniter:

<?php
    $session_id = $this->simple_sessions->get_sess_id();
?>

Retrieve value of session

To get a value of a session variable, you need to use the get_value($name) function, where name is the name of the variable in the sessions array. Note that if the variable is not existent, then this function will return false.

Normal PHP:

<?php 
    $fullname = $my_obj->get_value('fullname');
?>

Codeigniter:

<?php 
    $fullname = $this->simple_sessions->get_value('fullname');
?>

Check if variable exist

Sometimes all you need is to confirm if the session variable has been declared. To check if the variable is existent, then use the function check_sess($name). Again, $name is the variable name you want to look up. This function will return true or false.

Normal PHP:

<?php 
    $fullname_exists = $my_obj->check_sess('fullname');
?>

Codeigniter:

<?php 
    $fullname_exists = $this->simple_sessions->check_sess('fullname');
?>

Note that if you are going to look for a value of a session variable (get_value()), it is not necessary to use this function, as this same function calls check_sess().

Destroy the session

Lastly, the session variables need to be destroyed after they are not needed anymore. This can be achieved using the following function: destroy_sess(). My suggestion is, if you are going to just delete one session variable, then use del_sess(), otherwise, use destroy_sess() to delete the whole session.

Normal PHP:

<?php 
    // Destroy the session    
    $my_obj->destroy_sess();
?>

Codeigniter:

<?php 
    $this->simple_sessions->destroy_sess();
?>

That’s basically it for this sessions class. If you are using another PHP framework, and are able to use this class, please let me know how were you able to implement it, that way, I can add those instructions to here, and yes of course, you will get the credit for doing so.

Download link

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jotorres
Software Developer
Puerto Rico Puerto Rico
As a computer scientist, I strive to learn more everyday in my field. Everything I learn, I share with my community by writing articles for future references.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141030.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by jotorres
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid