Location: PHPKode > scripts > SessionManager > SessionManager.php
<?php
/*
*  Class SessionManager v.1.0
*  Developed by Rodrigo Cesar Vetulo
*  Date: 15/04/2003
*  Email: hide@address.com
*  URL: http://www.mosaic.com.br
*  (MosaicPortal's site, the brazilian opensource content management system)
* 
*  Description: If you use either PHP3.X or PHP4.X and needs to use
*  sessions, this class is for you. SessionManager is the easiest way
*  to handle variables in sessions.
* 
*  THIS CODE CAN BE FREELY DISTRIBUTED. PLEASE, DO NOT REMOVE THE AUTHORS INFO.
*  THANK YOU!
*/
class SessionManager{
	//private properties
	var $path;
	var $remoteIp;
	var $expiracao;
	
	// Constructor call: $session = new SessionManager("path_for_session", client_ip, expiration_time)
	//
	//		path_for_session: local directory where the session files will be stored
	//		client_ip: client ip used to handle sessiom, you can use $REMOTE_ADDR.
	//		expiration_time: the time in minutes that the session is going to be active. 
	function SessionManager($path_info, $ip, $exp)
	{
		$this->path = $path_info;
		$this->remoteIp = $ip;
		$this->expiracao = $exp;
	}

	//private methods
	function existVar($arq,$name)
	{
		$name = "\$$name";
		for($i = 1; $i < sizeof($arq)-1; $i++){
			$var = explode("=",$arq[$i]);
			if( $var[0]==$name )
				return $i;
		}
		return 0;
	}

	//public interface
	function createSession(){
		$data    = date("Ymd");
		$hora    = date("G") * 3600;
		$minuto  = date("i") * 60;
		$segundo = date("s");
		$tempo = $data + $hora + $minuto + $segundo + ($this->expiracao * 60);
		
		$arq = fopen($this->path . "session_" . $this->remoteIp . ".php", "w");
		fputs($arq,"<?\n"."\$expiracao=".$tempo.";"."\n?>");
		fclose($arq);
	}

	function registerSessionVar($name, $value)
	{
		$arq = file($this->path . "session_" . $this->remoteIp . ".php");

		$posExistente = $this->existVar($arq,$name);
		
		if( $posExistente!=0 ){
			$newValue = explode("=",$arq[$posExistente]);
			$newValue[1] = $value;
			$newValue = implode("=",$newValue);
			$arq[$posExistente] = $newValue . ";\n";

			$file = fopen($this->path . "session_" . $this->remoteIp . ".php", "w");
				$arq = implode("",$arq);
				exec("chmod -R 777 ".$this->path);
				fputs($file,$arq);
			fclose($file);
		} else {
			$arq[sizeof($arq)-1] = "\$$name=$value;\n";
			$arq[sizeof($arq)]   = "?>";

			$file = fopen($this->path . "session_" . $this->remoteIp . ".php", "w");
				$arq = implode("",$arq);
				exec("chmod -R 777 ".$this->path);
				fputs($file,$arq);
			fclose($file);
		}
	}
	
	function getSessionVar($name)
	{
		$arq = file($this->path."session_".$this->remoteIp.".php");

		$data    = date("Ymd");
		$hora    = date("G") * 3600;
		$minuto  = date("i") * 60;
		$segundo = date("s");
		$tempo   = $data + $hora + $minuto + $segundo;
		
		$exp = explode("=",$arq[1]);
		$exp = substr($exp[1],0,strlen($exp[1])-2);

		if($tempo > $exp)
			unlink($this->path . "session_" . $this->remoteIp . ".php");
		else {
			$this->registerSessionVar("expiracao", ($tempo + ($this->expiracao*60)));
			$name = "\$$name";
			for($i = 1; $i < sizeof($arq)-1; $i++){
				$var = explode("=",$arq[$i]);
				if( $var[0]==$name )
					return substr($var[1],0,strlen($var[1])-2);
			}
			return 0;
		}
	}
};
?>
Return current item: SessionManager