June 12, 2012 learn_php

Simple Sessions Class

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 explainatory, but nonetheless, I will take the time to document it.

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

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

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 libraries index.

In Controller:

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:


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:


Retrieve Session ID

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

Normal PHP:


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:


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:


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:


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.


Inactivity Timer

Hello all, I have added some functionality to this class. It allows you to verify user inactivity, and log them out if the inactivity time exceeds the max time designated. The “inactivity timer” is enabled when creating a new object of this class. The values should be sent through the constructor. I’ll explain how what each parameter is for:

Session Name

I recommend to all to have a different session name per project. This is, if there are sessions with the same name from different websites on the same browser, they can be somewhat mixed.


This is to enable or disable the inactivity timer. The default value is set to false, to have it disabled. Note how I passed the ‘TRUE’ value.


This number represents the max time you will allow your users to be inactive while in your website. These values are in seconds, so for an inactivity time of 5 minutes, the max would be set to 300(which is the default value).


This parameter is the page in which you have your login script. The default value is login.php, but this can be changed to your login page, which could be any name you have.

There you have it for now. Please leave feedback if you have used this class, and let me know where it can be improved.

Download link

Simple Sessions Class

4 thoughts on “Simple Sessions Class

  1. Hola, estaba revisando tu post (muy interesante la verdad) y decidí implementar tu clase como parte de un esquema MVC en PHP. La verdad es que soy nuevo en esto de PHP y me surge la duda de cómo recuperar las variables de sesión en otras clases. Me refiero a que según lo convencional llamas session_start() y ya verificas lo relacionado con $_SESSION[‘valor’], sin embargo como en el constructor de la clase la asignación esta como “$this->my_sess = $_SESSION;” no se de que forma llamar todo esto en un nuevo archivo php, ya que cuando lo hago obtengo un “Notice: A session had already been started – ignoring session_start() in…”. Saludos y espero me puedas orientar un poco.

    1. Saludos Ruben,

      Para obtener los datos de sesión utilizando esta clase, lo unico que necesitas hacer es lo siguiente:

      Vamos a suponer que ya las variables están creadas, y la variable la necesitas en el archivo a.php.

      Espero que esto te ayude.

      1. Gracias, voy a probar esto. Jeje te puse este mismo mensaje en inglés (borra el comentario si asi lo deseas). Saludos.

  2. So, if you don’t understand me on spanish this is what I want to say:

    Hi, I was watching this post (very interesting in deed) and I decided implement this class such a part of one MVC project on PHP. The ugly truth is that I’m noob in all this of PHP and I have one doubt about how get the session variables in external classes. I mean, in the PHP conventional programming we use session_start() and with this I can verify all related to session variables (like $_SESSION[‘value’]) but in the class constructor we have the assignation “$this->my_sess = $_SESSION;” which I don’t know how to use in external calls on PHP files… when I try to do it I get the message “Notice: A session had already been started – ignoring session_start() in…”. I hope you can help me with this. Thanks.

Leave a Reply

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

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.