preload
Apr 19

In PHP, sessions are pretty easy to work with since it just involves doing a few lines of code; initialize the session, define a session variable, then calling it back when you need it. PHP developers use sessions for passing data from page to page but shouldn’t it be a little more accountable then just passing data? A few examples would be data tracking, origin of variable, and what browser the user is in (for display purposes). This is valuable data for both the php coder and the web designer

Page1.php

// enable
session_start();
// define session variable
$_SESSION['test'] = 'good';

Page2.php

// start session again
session_start();
// display
echo $_SESSION['test']; // displays the word: good

That’s a very primitive way using sessions especially since were in php 5 (soon moving to php 6) and we can utilize classes to improve our usage of the $_SESSION variable.


http://www.phpclasses.org/browse/package/3307.html
is the source of a session management class and expands the capabilities of $_SESSION. When using this class, its no different from actually declaring a variable (like above) but more or so how sessions as a whole is used. Look for class.dbsession.php.

One of the most important features of this class is the fact it can track the user’s info while their session is active. This is key for manual logouts from a different user (admin), see who’s been logging on a person’s account and how long they were active, or simply show a who’s online function (display all rows). This is all stored in the database via MySQL. If a session is terminated the class has a garbage collector and deletes any records that have not been updated within a specified amount of time. When I use this class, I set $session->gc(60*15);, so all records that are inactive for 15 minutes are deleted.

Try out the class, it’ll add a lot of neat features to your site, admin and public side. The class has comments all over so its easy directions in how-to-use.
Page1.php

//session class and start sessions
require_once("classes/class.dbSession.php");
$session = new dbSession();
//run session garbage collector (15 minutes is how much it runs)
$session->gc(60*15);
// define session variable
$_SESSION['test'] = 'good';

Page2.php

require_once("classes/class.dbSession.php");
$session = new dbSession();
$session->gc(60*15);
// display
echo $_SESSION['test']; // displays the word: good
  • Share/Bookmark

4 Responses to “Proper Session Handling”

  1. css gallery Says:

    You are a very smart person!

  2. zack Says:

    Hi, nice post. I have been pondering this issue,so thanks for writing. I will certainly be coming back to your site.

  3. KrisBelucci Says:

    Hi, cool post. I have been wondering about this topic,so thanks for writing.

  4. GarykPatton Says:

    You know so many interesting infomation. You might be very wise. I like such people. Don’t top writing.

Leave a Reply