home *** CD-ROM | disk | FTP | other *** search
- HSession
-
- Session management webapplication oriented. Variable registration from form or global, lifetime for each application, ...
-
-
- --------------------------------------------------------------------------------
- <?php
- // -------------------
- // HH 200004181107v0.2
- // Herve HERITIER
- // Hsession (file)
- // -------------------
-
- if (!defined( "__PHP_SESSION")) {
- define ( "__PHP_SESSION",1);
- // Directory for sessions files
- define ( "PHP_SESSIONDIR", "/var/tmp/phpsession/");
- // configuration file
- // inf file contain on line by application
- // for each application, three parameters, comma delimited
- // application name, path for cookie, timeout
- //
- define ( "PHP_SESSIONINF", "/etc/httpd/conf/phpsessions.inf");
- define ( "COOKIE_DOMAIN", getenv( "SERVER_NAME"));
-
-
- class Csession {
- var $inf,$firstcall;
-
- // Private
-
- function sendCookie ($value= "@#&") {
- if ($value == "@#&")
- $value = $this->cookie;
- setcookie($this->name, $value, 0, $this->cookiepath,COOKIE_DOMAIN);
- }
-
- // Private
- 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);
- }
-
- // Public
- function destructor() {$this->save();}
- // Public
- 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();
- }
- // Public
- 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);
- }
- // Public
- // Reference a global variable into the session
- function register($var) {
- if (!isset($this->sdata[$var]))
- $this->sdata[$var] = $var;
- }
- // Public
- // unreference a global variable into the session
- 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];
- }
- // Public
- // Reference all POST variables from a form
- // except the exceptvars list (space delimited)
- 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);
- }
- }
- // Public
- // clear the session
- function destroy() {
- @unlink($this->sfile);
- unset($this->sdata);
- }
-
- // Public
- // save the registred variable to the session file
- 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
- }
- ?>
-
-