Login   Register  
PHP Classes
elePHPant
Icontem

File: session.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tom Du  >  postgreSQL_session  >  session.php  >  Download  
File: session.php
Role: ???
Content type: text/plain
Description: session class
Class: postgreSQL_session
Author: By
Last change:
Date: 13 years ago
Size: 4,188 bytes
 

Contents

Class file image Download
<?php
/*
// sessioncookie : session management
//
// Original implementation by:
// Danny Heijl - Danny.Heijl@cevi.be, aug 1999
//
// Modified and enhanced by:
// Christoph Kassen - php@chkassen.de, nov 2000
// Download at www.chkassen.de
//
// Convert to use PostgreSQL, modify and enhanced by:
// Thomas Du - konga.geo@yahoo.com, Jan. 2000
//
// USE THIS SOFTWARE AT YOUR OWN RISK
//
// --------------------------------------------------------------------
*/
class sessioncookie
{
 var $sdata;
 var $id;
 var $dblink;
 //Constructor
 //Must be called before any output is generated 
 function sessioncookie () {
  global $php_sessid;

  //Disable caching
  header ("Expires: Mon, 26 Jul 1999 05:00:00 GMT");    // Date in the past
  header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  // always modified
  header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  header ("Pragma: no-cache");                          // HTTP/1.0

  $this->id = $php_sessid;
  $this->sdata = array();
  $this->dblink = '';
 }
 
 //Name   : connect2db
 //Description: Opens a connection to the mySQL server and selects a DB
 //Returns    : -
 //Parameter  : -
 function connect2db (){
  $host = "localhost";
  $port = "5432";
  $db = "database";

  $this->dblink = @pg_connect("dbname=$db host=$host port=$port") or die ("Could not connect to DB.\n");
 }
 
 //Name   : start
 //Description: Starts a new session or loads previously stored values for a session
 //Returns    : -
 //Parameter  : -
 function start () {
  if(!isset($this->id)) {
   //no session id found
   //generate a new one
   srand ((double) microtime() * 1000000);
   $this->id = md5(uniqid(rand()));
   setcookie("php_sessid", $this->id, 0);
   return;
  } else {
   //session id found
   $query = "SELECT val FROM session WHERE sid='$this->id';";
   $result = @pg_exec($this->dblink, $query);
      $sessiondata = @pg_fetch_row($result, 0);
   $rest = unserialize($sessiondata[0]);
   while (@list($key, $val) = @each($rest)) {
    $GLOBALS["$key"] = $val;
    $this->sdata[$key] = $val;
   }
  }
 }

 //Name   : register
 //Description: Registers a session variable
 //Returns    : -
 //Parameter  : variablename
 function register ($var) {
  $this->sdata[$var] = $var;
 }

 //Name   : unregister
 //Description: Unregisters a session variable
 //Returns    : -
 //Parameter  : variablename
 function unregister ($var) {
  unset($this->sdata[$var]);
  unset($GLOBALS["$var"]);
 }

 //Name   : destroy
 //Description: Stops a current session
 //Returns    : -
 //Parameter  : -
 function destroy () {
  while (@list($key, $val) = @each($this->sdata)) {
   $this->unregister($key);
  }
  $this->sdata = array();
  $this->save();
  $this->id = '';
  $php_sessid = '';
  // closing database persistent connection
  pg_close($this->dblink);
  $this->dblink = '';
 }

 //Name   : isregistered
 //Description: Checks if a variable is registered in a session
 //Returns    : 1(true) or 0(false)
 //Parameter  : variablename
 function isregistered ($name) {
  return (isset($this->sdata[$name]) ? 1 : 0);
 }

 //Name   : save
 //Description: Saves the session array
 //Returns    : -
 //Parameter  : -
 function save () {
  for(@reset($this->sdata); $var = @key($this->sdata); @next($this->sdata)) {
   $ts[$var] = $GLOBALS[$var];
  }

  $ts = serialize($ts);
  $sqldate = date("YmdHis", time());

  $query = "UPDATE session SET val='$ts', changed=$sqldate WHERE sid='$this->id';";
  $result = @pg_exec($this->dblink, $query);
  if(@pg_cmdtuples($result) == 0) {
   $query = "INSERT INTO session (sid, val, changed) VALUES ('$this->id', '$ts', $sqldate);";
   $result = @pg_exec($this->dblink, $query);
    }
 }

 //Name   : gc
 //Description: Deletes sessions older than the gctime value from the database
 //Returns    : -
 //Parameter  : Time in seconds
 function gc ($gctime) {
  $sqldate = date("YmdHis", time() - ($gctime * 60));
  $query = "DELETE FROM session WHERE changed < '$sqldate';";
  $result = @pg_exec($this->dblink, $query);
 }

} //Class end \\
?>