<?php
if (!defined("__PHP_SESSION")) {
define ("__PHP_SESSION",1);
define ("PHP_SESSIONDIR","/var/tmp/phpsession/");
define ("PHP_SESSIONINF", "/etc/httpd/conf/phpsessions.inf");
define ("COOKIE_DOMAIN", getenv("SERVER_NAME"));
class Csession {
var $inf,$firstcall;
function sendCookie ($value="@#&") {
if ($value == "@#&")
$value = $this->cookie;
setcookie($this->name, $value, 0, $this->cookiepath,COOKIE_DOMAIN);
}
function loadInf () {
$fp = fopen (PHP_SESSIONINF, "r");
if (! $fp) {
return;
}
flock($fp, 1);
while ($line = trim(fgets($fp, 2048))) {
$buffer = explode(",", $line);
$this->inf[$buffer[0]]["path"]=$buffer[1];
$this->inf[$buffer[0]]["timeout"]=$buffer[2];
}
fclose($fp);
}
function destructor() {$this->save();}
function Csession($newname = "PHPSESSID", $forcenew=0) {
$this->sessid = "";
$this->sdata = array();
$this->sfile = "";
$this->cookie = "";
$this->cookiepath = "/";
$this->firstcall = 0;
$this->forcenew = $forcenew;
$this->client = $GLOBALS["REMOTE_ADDR"];
$this->name = $newname;
$this->sessid = $this->name . date("YmdHis") . "_" . str_replace(".", "_", $this->client);
$this->sfile = PHP_SESSIONDIR . $this->sessid;
$this->cookie = $this->sessid;
//register_shutdown_function("session_save");
$this->loadInf();
$this->cookiepath = $this->inf[$this->name]["path"];
$this->timeout=$this->inf[$this->name]["timeout"];
$this->start();
}
function start () {
global $HTTP_COOKIE_VARS;
$php_sessid = $HTTP_COOKIE_VARS[$this->name];
if ($php_sessid == "") {
$this->sendCookie();
$this->firstcall = true;
return;
}
else {
if (file_exists(PHP_SESSIONDIR . $php_sessid)) {
$a_stats = stat(PHP_SESSIONDIR . $php_sessid);
$delay = time() - $a_stats["9"];
if ($delay > $this->timeout) {
@unlink (PHP_SESSIONDIR . $php_sessid);
$this->firstcall = true;
$this->sendCookie();
return;
}
}
if ($this->forcenew) {
@unlink (PHP_SESSIONDIR . $php_sessid);
$this->sendCookie();
$this->sendCookie("");
$this->firstcall = true;
return;
}
else {
$this->cookie = $php_sessid;
$this->sessid = $php_sessid;
$this->sfile = PHP_SESSIONDIR . $php_sessid;
}
}
@$fp = fopen($this->sfile, "r");
if (! $fp) {
$this->firstcall = true;
$this->sendCookie();
return;
}
flock($fp, 1); // get shared lock and wait
$line = trim(fgets($fp, 2048));
if ($line != $php_sessid) {
fclose($fp);
$this->sendCookie($php_sessid);
return;
}
else
$this->cookie = $php_sessid;
while ($line = fgets($fp, 2048)) {
list($name, $svar) = explode("=", $line);
$GLOBALS[$name] = unserialize($svar);
$this->sdata[$name] = $name;
}
fclose($fp);
}
function register($var) {
if (!isset($this->sdata[$var]))
$this->sdata[$var] = $var;
}
function unregister($var) {
unset ($this->sdata[$var]);
}
function registerFromPost ($var) {
global $HTTP_POST_VARS;
$this->sdata[$var] = $HTTP_POST_VARS[$var];
$GLOBALS[$var] = $this->sdata[$var];
}
function registerAllFromPost ($exceptvars="") {
global $HTTP_POST_VARS;
$exceptvars = " $exceptvars ";
reset ($HTTP_POST_VARS);
while (list ($var,$value) = each($HTTP_POST_VARS)) {
if (strpos($exceptvars, $var . " ") == 0)
$this->registerFromPost ($var);
}
}
function destroy() {
@unlink($this->sfile);
unset($this->sdata);
// $this->sendCookie("");
}
function save() {
if (! is_array($this->sdata))
return;
if (file_exists($this->sfile)) {
$fp = fopen($this->sfile, "r+");
}
else {
$fp = fopen($this->sfile, "w");
}
flock($fp, 2); // exclusive lock and wait
fputs($fp, trim($this->cookie) . "\n");
$i = 0;
reset($this->sdata);
while ($i++ < count($this->sdata)) {
$var = key($this->sdata);
$data = $GLOBALS[$var];
$svar = serialize($data);
fputs($fp,$var . "=" . $svar . "\n");
next($this->sdata);
}
fclose($fp);
}
} // end of class session
}
?> |