Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Simple Sessions Class

0.00/5 (No votes)
9 Oct 2012CPOL3 min read 8.9K   13  
This is 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 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
<?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
<?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
<?php
    $autoload['libraries'] = array('simple_sessions');
?>
In Controller
PHP
<?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
<?php 
    $my_obj = new Simple_sessions();
    $data = array(
                'username'     => $username,
                'userid'     => $userid,
                'fullname'     => $fullname,
                'status'     => $status
            );
    $my_obj->add_sess($data);
?>

Codeigniter

PHP
<?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
<?php 
    // To edit fullname
    $my_obj->edit_sess('fullname', 'My New Name');
    // To delete status 
    $my_obj->del_sess('status');
?>

Codeigniter

PHP
<?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
<?php
    $session_id = $my_obj->get_sess_id();
?>

Codeigniter

PHP
<?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 existant, then this function will return false.

Normal PHP

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

Codeigniter

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

Check If Variable Exists

Sometimes, all you need is to confirm if the session variable has been declared. To check if the variable is existant, 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
<?php 
    $fullname_exists = $my_obj->check_sess('fullname');
?>

Codeigniter

PHP
<?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 needs 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
<?php 
    // Destroy the session    
    $my_obj->destroy_sess();
?>

Codeigniter

PHP
<?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 you were 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.

License

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