<?php
// session.php3 : session management
//
// Original implementation by:
// Danny Heijl - Danny.Heijl@cevi.be, aug 1999
// Modified and enhanced by:
// Christoph Kassen - php@chkassen.de, dec 1999
// Converted to use ORACLE7/8 instead of MySQL by:
// Richard Fairthorne - richardf@lbcnetworks.com, jul 2000
// SQL converted by Sean Ford - seanf@lbcnetworks.com
// This can be used as a drop in replacement for the original
// session.php3 if you are switching (for whatever reason)
// to Oracle7 or 8 or 8i.
// Because oracle will not allow a table name 'session',
// I use the name sessions throughout.
// Warning.. it makes $dblink global.. oh well.
// USE THIS SOFTWARE AT YOUR OWN RISK
//
// --------------------------------------------------------------------
//Name : connect2db
//Description: Opens a connection to the Oracle7/8 server and selects a DB
//Returns : -
//Parameter : -
function connect2db(){
global $dblink;
$username="user";
$password="pass";
$db="dbname";
$dblink = OCILogon ("$username", "$password","$db");
if (!$dblink){
echo "Could not connect to database\n";
exit;
}
// mysql_select_db( "your database", $dblink); Functionality duplicated in OCILogin Function
}
class sess {
function sess() {
$this->sessid = "";
$this->sdata = array();
$this->cookie = "";
$this->sessid = md5(uniqid(rand()));
$this->sdate = date("YmdHis", time());
$this->cookie = $this->sessid;
}
}
//Name : s_start
//Description: Starts a new session or loads previously stored values for a session
//Returns : -
//Parameter : -
function s_start() {
global $dblink;
global $session;
global $php_sessid;
$session = new sess();
if (! isset($php_sessid)){
setcookie("php_sessid", $session->cookie, "", "/");
$php_sessid = $session->cookie;
return;
}
else {
$session->cookie = $php_sessid;
}
$query = "select * from sessions where sid = '$php_sessid'";
$result = OCIExecute (OCIParse ($dblink, $query));
if (! $result) {
$php_sessid = $session->cookie;
setcookie("php_sessid", $session->cookie, "", "/");
return;
}
$session->cookie = $php_sessid;
$query = "select VAL from sessions where sid = '$php_sessid'";
$stmt=OCIParse ($dblink, $query);
$result = OCIExecute ($stmt);
@OCIFetchInto($stmt, &$row,OCI_ASSOC+OCI_RETURN_NULLS);
list($name, $svar) = explode("=", $row['VAL']);
$GLOBALS[$name] = unserialize($svar);
}
//Name : s_register
//Description: Registers a session variable
//Returns : -
//Parameter : variablename
function s_register($var) {
global $session;
$session->sdata[$var] = $var;
}
//Name : s_unregister
//Description: Unregisters a session variable
//Returns : -
//Parameter : variablename
function s_unregister($var) {
global $session;
unset ($session->sdata[$var]);
}
//Name : s_destroy
//Description: Stops a current session
//Returns : -
//Parameter : -
function s_destroy() {
global $session;
unset ($session->sessid);
unset ($session->sdata);
unset ($session->cookie);
unset($session);
}
//Name : s_isregistered
//Description: Checks if a variable is registered in a session
//Returns : 1(true) or 0(false)
//Parameter : variablename
function s_isregistered($name) {
global $session;
if(isset($session->sdata[$name])){
return 1; //$name is registered
}
else{
return 0; //$name is not registered
}
}
//Name : s_save
//Description: Saves the session array
//Returns : -
//Parameter : -
function s_save() {
global $dblink;
global $session;
for (reset($session->sdata); $var = key($session->sdata); next($session->sdata)) {
$data = $GLOBALS[$var];
$svar = serialize($data);
$ts .= $var ."=". $svar;
}
$query = "Insert into sessions (sid, val, changed ) values('${session->cookie}', '${ts}','${session->sdate}')";
$stmt = OCIParse($dblink, $query);
$result = @OCIExecute ($stmt);
if(! $result){
$query = "Update sessions set val = '$ts' where sid = '$session->cookie'";
$result = OCIExecute (OCIParse ($dblink, $query));
}
}
//Name : s_gc
//Description: Deletes sessions older than the gctime value from the database
//Returns : -
//Parameter : Time in seconds
function s_gc($gctime){
global $dblink;
global $session;
$timeout = time();
$sqldate = date("YmdHis", $timeout - ($gctime * 60));
$query = "DELETE FROM sessions WHERE changed < '$sqldate'";
$res = OCIExecute (OCIParse ($dblink, $query));
}
?> |