Hi, Guest ~ Login or Register

Custom way to implement sessions.

Posted in Website Programming » PHP - Thursday 20th September 2007 at 7:09PM

itunes66
Member

User Avatar

Joined April 2007
Posts: 22

How many of you have, or would, create their own class to handle sessions, and i don't mean native php session support, i mean completely from scratch.

I just did (stores them in a database), the problem is with the way php handles things, it calls all destructors and then closes the session, well if your using MySQLi (the OOP part of it) or your own database class, this is bad, so i just wrote my own sessions class, it handles: creating, destroying, ID Generation, Garbage Collection, and continuing sessions.

how far would you go to do this, and how about some comments about the php behavior as well.

__________________________________

 

Replies (6)

Replied - Saturday 22nd September 2007 at 4:53PM [Post Link]

Will
Administrator

User Avatar

Joined October 2005
Posts: 133

I haven't used MySQLi yet but I look forward to using it, or its equivalent in PHP6 when I have the time to start something new.

Personally I use my own database class instead of anything too flashy, it gets the job done and tells me what I want to know if I need to debug anything.

Could we see a few examples of your sessions handling class? I've gotta say it's nothing I've thought about before but it does seem quite interesting, even though sessions support is pretty well handled without the need for any OOP.

__________________________________
Will Morgan
Freelance Web Developer
Next feature: How to fit 25 hours into a day!

 
Replied - Saturday 22nd September 2007 at 8:18PM [Post Link]

itunes66
Member

User Avatar

Joined April 2007
Posts: 22

sure, ill have to transfer it to this computer though, and I agree but i needed the support for MySQLi because i did not have the time to re-code mine, and i like the OOP feel of it as well, php session support is great but i want mine in a mysql database and that is why.

Update:

Here is the Example (it's not commented though) and this is the actual script I use, i try to be as secure as possible with it.


<?php
CLASS Session {
  protected $id;
  protected $name;
  protected $maxlife;
  protected $data;
  function __construct($name='sid') {
    $this->name = $name;
    $this->maxlife = 3600;
    $this->gc();
  }
  function begin() {
    if ($this->sessionExists()) {
      // Proceed
    } else {
      $this->create();
    }
  }
  function create() {
    GLOBAL $db;
    $this->generateID();
    setcookie($this->name, $this->id, time() $this->maxlife);
    $access = $this->escape(time());
    $db->query('INSERT INTO sessions (sess_id, data, access) VALUES('' . $this->id . '', '', '' . $access . '')');
  }
  function destroy() {
    GLOBAL $db;
    $db->query('DELETE FROM sessions WHERE sess_id='' . $this->id . ''');
  }
  function write() {
    GLOBAL $db;
    $this->updateAccess();
    $sData = $this->serializeData();
    $db->query('UPDATE sessions SET data='' . $sData . '' WHERE sess_id='' . $this->id . ''');
  }
  function generateID() {
    $this->id = $this->escape(sha1(uniqid('sess_') . time() . sha1('mc_' . microtime())));
  }
  function sessionExists() {
    GLOBAL $db;
    if (isset($_COOKIE[$this->name])) {
      $sessid = $this->escape($_COOKIE[$this->name]);
      $q = $db->query('SELECT data FROM sessions WHERE sess_id = '' . $sessid . ''');
      if ($q->num_rows == 1) {
        $this->id = $sessid;
        $fetch = $q->fetch_array();
        $this->data = unserialize($fetch['data']);
        $this->updateAccess();
        return TRUE;
      }
    }
    return FALSE;
  }
  function updateAccess() {
    GLOBAL $db;
    $access = $this->escape(time());
    $db->query('UPDATE sessions SET access='' . $access . '' WHERE sess_id='' . $this->id . ''');
  }
  function escape($mv) {
    GLOBAL $db;
    return $db->real_escape_string($mv);
  }
  function setVar($name, $value) {
    $this->data[$name] = $value;
  }
  function getVar($name) {
    if ($this->varExists()) {
      return $this->data[$name];
    } else {
      return NULL;
    }
  }
  function unsetVar($name) {
    if ($this->varExists()) {
      unset($this->data[$name]);
    }
  }
  function varExists($name) {
    if (isset($this->data[$name])) {
      return TRUE;
    }
    return FALSE;
  }
  public function getAllData() {
    return $this->data;
  }
  private function serializeData() {
    return serialize($this->data);
  }
  function __destruct() {
    $this->write();
  }
  function gc() {
    GLOBAL $db;
    $chance = mt_rand(0, 10);
    if ($chance == 10) {
      $old = time() - $this->maxlife;
      $db->query('DELETE FROM sessions WHERE access < ' . $old);
    }
  }
}
$sess = new Session('sid');
function sess_begin() {
  GLOBAL $sess;
  $sess->begin();
}
function sess_get($name) {
  GLOBAL $sess;
  return $sess->getVar($name);
}
function sess_getAllData() {
  GLOBAL $sess;
  return $sess->getAllData();
}
function sess_unset($name) {
  GLOBAL $sess;
  $sess->unsetVar($name);
}
function sess_set($name, $value) {
  GLOBAL $sess;
  $sess->setVar($name, $value);
}
function sess_isset($name) {
  GLOBAL $sess;
  $sess->varExists($name);
}
function sess_destroy() {
  GLOBAL $sess;
  $sess->destroy();
}
?>

__________________________________

 
Sponsored Link
Replied - Saturday 22nd September 2007 at 9:39PM [Post Link]

Will
Administrator

User Avatar

Joined October 2005
Posts: 133

So really, instead of saving the data in the memory you're just saving it in MySQL.

__________________________________
Will Morgan
Freelance Web Developer
Next feature: How to fit 25 hours into a day!

 
Replied - Saturday 22nd September 2007 at 11:42PM [Post Link]

itunes66
Member

User Avatar

Joined April 2007
Posts: 22

pretty much.

__________________________________

 
Replied - Sunday 23rd September 2007 at 5:56AM [Post Link]

Will
Administrator

User Avatar

Joined October 2005
Posts: 133

..and the advantages are..?

__________________________________
Will Morgan
Freelance Web Developer
Next feature: How to fit 25 hours into a day!

 
Replied - Sunday 23rd September 2007 at 1:21PM [Post Link]

itunes66
Member

User Avatar

Joined April 2007
Posts: 22

for storing in mysql, an easier who is online function, page tracking, etc..., without the need for another table to store data in.

__________________________________