session.lib.php
上传用户:jiangbw
上传日期:2022-03-16
资源大小:49k
文件大小:16k
- <?
- /*======================================================================*
- || #################################################################### ||
- || # BUILD UNDER PHP SCRIPTNET 3.2.1 FRAMEWORK ||
- || # ---------------------------------------------------------------- # ||
- || # Code2Art Open Source Software. All Rights Reserved. ||
- || # This file should be redistributed in whole or significant part. # ||
- || # ------------------ SCRIPTNET IS FREE SOFTWARE ------------------ # ||
- || # http://www.code2art.com | http://www.code2art.com/scriptnet # ||
- || # Copyleft by Benediktus Ardian Hersanto, SE # ||
- || # Sorry if this framework is still unavailable for public # ||
- || # because I still searching on my own head. # ||
- || #################################################################### ||
- *======================================================================*/
- /*======================================================================*
- || #################################################################### ||
- || DBF to MySQL Bulk Converter 2.0 ||
- ||______________________________________________________________________||
- || This utility was developed on 2006, since this were used to be on ||
- || my own library. But I think I should share it to you, because I ||
- || need to save my work on Internet (I don't believe on my own HD drive)||
- || it always crash and had a terrible bad sectors. ||
- || Thanks to all person who download this utility, I hope you enjoy it. ||
- || ||
- || Regards, ||
- || Benediktus Ardian Hersanto,SE (ardie_b@yahoo.com) ||
- /*======================================================================*/
- // ---------------------------------------------------------------------//
- // Free to use for everyone who understand PHP
- // ---------------------------------------------------------------------//
- // Session.Lib.Php is a part of PHP SCRIPTNET FRAMEWORK
- // LAST UPDATE : 28.07.08
- // Changed :
- // Set Cookie are handled through header command not using standard PHP
- // Added Save Handler methods for PHP4 Session (the session file reside on web dir)
- // Added Function session_unregister($name)
- // Added Function for limiting cookie lifetime - PHP Session
- // Added Function for limiting cookie lifetime - FILE Text Session
- //
- // Dependency :
- // - init.php (Environtment setup library)
- // - config.ini.php (Configuration Settings)
- //
- // Still on Development
- // - SESSION Based on Database Server
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- /* NOTES on php.ini settings due to hardening security on web server
- - if session.use_only_cookies is disabled and session.use_cookies is disabled so
- you have to enable session.use_trans_sid to easing your life.
- /*===================================================================================*/
- // SESSION TYPES SUPPORTED BY THIS CLASS
- define('SESS_TYPE_FILE',0x01);
- define('SESS_TYPE_DB',0x02);
- define('SESS_TYPE_PHP',0x03);
- /* GENERAL FUNCTION FOR SESSION CLASS TO WORK
- ANY OF THESE FUNCTION ARE NOT AVAILABLE ON PHP 3 */
-
- /* SESSION ACTION PROCEDURE
- 1. Generate ID to identify each clients
- 2. Create Temporary file to store each clients data
- */
-
- if(!function_exists('open')&&!function_exists('close')&&!function_exists('read')) {
- function open($sess_save_path='', $session_name='')
- {
- //$sess_save_path, $sess_session_name;
- $sess_save_path = ROOT_DIR.SESS_TMP_DIR;//$save_path;
- return true;
- }
-
- function close()
- {
- return true;
- }
-
- function read($id)
- {
- //$sess_save_path, $sess_session_name;
-
- $sess_file = $sess_save_path."/sess_".$id;
- if(file_exists($sess_file)) {
- $fp = fopen($sess_file,"r");
- if($fp) {
- if(filesize($sess_file)>0)
- return fread($fp,filesize($sess_file));
- else return;
- }
- fclose($fp);
- } else return;
- }
-
- function write($id, $sess_data)
- {
- //global $sess_save_path, $sess_session_name;
- $sess_file = $sess_save_path."/sess_".$id;
- $fp = fopen($sess_file, "w");
- if ($fp) {
- if(fwrite($fp, $sess_data)===FALSE) { echo "cannot write to session file";return false;}
- fclose($fp);
- return true;
- } else {
- return false;
- }
- }
-
- function destroy($id)
- {
- //global $sess_save_path, $sess_session_name;
-
- $sess_file = $sess_save_path."/sess_".$id;
- return(@unlink($sess_file));
- }
-
- /*********************************************
- * WARNING - You will need to implement some *
- * sort of garbage collection routine here. *
- *********************************************/
- function gc($maxlifetime=0)
- {
- return true;
- }
- }
-
- class session extends dynamicClass {
-
- var $sess_name = "PHPSESSID";
- var $sess_file=null;
- var $type = SESS_TYPE_FILE;
- var $id;
- var $_varname = 'HTTP_SESSION_VARS';
- var $security_enabled = false;
-
- function session() {
- global $sess_session_name;
- if((ini_get('session.use_only_cookies')=='0')&&(ini_get('session.use_cookies')=='0')) {
- $this->security_enabled = true;
- }
- $this->directory = ROOT_DIR.SESS_TMP_DIR;
- if(trim(STORAGE_TYPE)=='SESS_FILES') $this->type = SESS_TYPE_FILE;
- else if(trim(STORAGE_TYPE)=='PHP_DEFAULT') $this->type = SESS_TYPE_PHP;
- else if(trim(STORAGE_TYPE)=='SESS_DB') $this->type = SESS_TYPE_DB;
- if(USE_PHP_DEFAULT_HANDLER) $this->type = SESS_TYPE_PHP;
-
- if(!is_dir($this->directory)) if(!mkdir($this->directory,0777)) trigger_error('session's temporary directory "{$this->directory}" is read-only or does not exist',E_USER_WARNING);
- if (PHPVERSION<4.01) $this->type=SESS_TYPE_FILE; //DEFAULT SESSION TYPE FOR PHP 3
- else $this->_varname = ((PHPVERSION<4.10)?'HTTP_SESSION_VARS':'_SESSION');
-
- if ($this->type==SESS_TYPE_PHP) {
- $this->sess_name = ((!defined('SESS_NAME'))?$this->sess_name:SESS_NAME);
- session_name($this->sess_name);
- // Override Cookie Data Handling
- if(defined('USE_PHP_DEFAULT_HANDLER')&&(!USE_PHP_DEFAULT_HANDLER)) {
- session_save_path($this->directory);
- session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
- }
- /*echo "The previous session name was $name<br />Now is ",session_name();
- echo "<BR>";*/
- if(!isset($GLOBALS[$this->_varname])) {
- if(!defined('COOKIE_LIFETIME')) define('COOKIE_LIFETIME',5);
- if(!defined('COOKIE_PATH')) define('COOKIE_PATH','/');
- if(defined('COOKIE_DOMAIN')&&COOKIE_DOMAIN!='') {
- if(defined('COOKIE_SECURE')&&COOKIE_SECURE!=0) {
- if(!$this->security_enabled)
- session_set_cookie_params(time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH,COOKIE_DOMAIN,1);
- } else {
- if(!$this->security_enabled)
- session_set_cookie_params(time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH,COOKIE_DOMAIN);
- }
- } else {
- if(!$this->security_enabled)
- session_set_cookie_params(time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH);
- }
- if(!session_start()) die('Cannot start session');
- } else if(!session_start()) die('Cannot start session');
- } else $this->sess_name = (defined('SESS_NAME')?SESS_NAME:$this->sess_name);
- $cookie = $this->_getCookie($this->sess_name); // get PHPSESSID md5 string
- if(!empty($cookie)) $this->id = $cookie; else $this->id = session_id();
- if($this->security_enabled) output_add_rewrite_var($this->sess_name,$this->id);
- if(!USE_PHP_DEFAULT_HANDLER) {
- if(empty($cookie)&&($this->type==SESS_TYPE_FILE)) $this->generate_id();
- else $this->sess_file = $this->directory."/sess_".$cookie;
- }
-
- if(!empty($cookie)&&($this->type==SESS_TYPE_FILE)) $GLOBALS[$this->_varname] = $this->_getRegisteredSessions();
- /*if ($this->type==SESS_DB)
- {
- // not defined yet
- }*/
- if(!defined("SESSION_STARTED")) define("SESSION_STARTED",true); // LOADED FLAG
- }
-
- function register($name) {
- $cookie = $this->_getCookie($this->sess_name);
- if(empty($cookie)) {
- if(!$this->security_enabled) {
- if (PHPVERSION<4.01) {
- echo '<b>Notice:</b> Session is not yet started.';
- }
- else if (PHPVERSION>4.01) {
- trigger_error("Session is not yet started.",E_USER_NOTICE);
- }
- } else {
- if($this->type==SESS_TYPE_PHP) {
- $GLOBALS[$this->_varname][$name] = $GLOBALS[$name];
- }
- }
- } else {
- if(is_string($name)) {
- if($this->type==SESS_TYPE_FILE) {
- if(file_exists($this->sess_file)) unlink($this->sess_file);
- $fid = fopen($this->directory."/sess_".$this->_getCookie($this->sess_name),"w");
- if(!$fid) return false;
- fclose($fid);
- $this->sess_file = $this->directory."/sess_".$this->_getCookie($this->sess_name);
- chmod($this->sess_file,0777);
- }
- if(isset($GLOBALS[$this->_varname][$name])) unset($GLOBALS[$this->_varname][$name]);
- $GLOBALS[$this->_varname][$name] = $GLOBALS[$name];
- } else if(is_array($name)) {
- while(list($k,$v)=each($name)) {
- if(isset($GLOBALS[$this->_varname][$k])) unset($GLOBALS[$this->_varname][$k]);
- $GLOBALS[$this->_varname][$k] = $v;
- }
- } else if(is_object($name)) {
- if(PHPVERSION<4.01) {
- echo '<b>Notice:</b> Parameter Object only available for PHP 4 or later';
- } else {
- $data = get_object_vars($name);
- if(count($data)>0) {
- while(list($k,$v)=each($data)) {
- if(isset($GLOBALS[$this->_varname][$k])) unset($GLOBALS[$this->_varname][$k]);
- $GLOBALS[$this->_varname][$k] = $v;
- }
- }
- }
- }
- }
- if($this->type==SESS_TYPE_FILE) $this->_write($this->sess_file);
- }
-
- function unregister($name) {
- if(isset($GLOBALS[$this->_varname][$name])) unset($GLOBALS[$this->_varname][$name]);
- }
-
- function vars($name) {
- if(isset($GLOBALS[$this->_varname][$name])) return $GLOBALS[$this->_varname][$name]; else return null;
- }
-
- function id($str='') {
- if(empty($str)) {
- return $this->_getCookie($this->sess_name);
- } else {
- setcookie($this->sess_name,$str);
- if($this->type==SESS_TYPE_FILE) {
- $fid = fopen($this->directory."/sess_".$str,"w");
- fclose($fid);
- if(file_exists($this->sess_file)) unlink($this->sess_file);
- $this->sess_file = $this->directory."/sess_".$str;
- }
- }
- }
-
- function destroy() {
- $this->_unset();
- if(isset($GLOBALS[$this->_varname])) unset($GLOBALS[$this->_varname]);
- if($this->type==SESS_TYPE_FILE) {
- if(file_exists($this->sess_file)) {
- unlink($this->sess_file);
- }
- }
- }
-
- function is_registered($name) {
- $data = $this->_getRegisteredSessions();
- if(isset($data[$name])) return true; else return false;
- }
-
- function _unset() {
- if(($this->type==SESS_TYPE_PHP)&&(USE_PHP_DEFAULT_HANDLER)) {
- if(count($GLOBALS[$this->_varname])>0) {
- unset($GLOBALS[$this->_varname]);
- }
- session_destroy();
- } else {
- if(!USE_PHP_DEFAULT_HANDLER)
- if($this->_getCookie($this->sess_name)!='') setcookie($this->sess_name,'',time()-7200);
- }
- }
-
- function generate_id() {
- $id = $this->_generate_id();
- if(!defined('COOKIE_LIFETIME')) define('COOKIE_LIFETIME',5);
- if(!defined('COOKIE_PATH')) define('COOKIE_PATH','/');
- if(defined('COOKIE_DOMAIN')&&trim(COOKIE_DOMAIN)!='') {
- if(defined('COOKIE_SECURE')&&COOKIE_SECURE!=0) {
- if(!setcookie($this->sess_name,$id,time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH,COOKIE_DOMAIN,1)) echo('<b>Could not send cookies to browser</b>');
- //header("Set-Cookie: {$this->sess_name}={$id}; expires=".gmdate("M d Y H:i:s", time()+(60*60*((1/60)*COOKIE_LIFETIME)))." GMT; path=".COOKIE_PATH."; domain=".COOKIE_DOMAIN."; secure");
- if (PHPVERSION<4.10) $GLOBALS['HTTP_COOKIE_VARS'] = array($this->sess_name=>$id);
- else $GLOBALS['_COOKIE'] = array($this->sess_name=>$id);
- //Set-Cookie: cookie[tiga]=cookieone; expires=Sun, 18-Feb-2007 17:26:51 GMT; path=/UAM_Server/; domain=192.168.1.236; secure
- //setcookie($this->sess_name,$this->_generate_id(),time()+(60*60*((1/60)*COOKIE_LIFETIME)),COOKIE_PATH,COOKIE_DOMAIN,1);
- } else {
- //header("Set-Cookie: {$this->sess_name}={$id}; expires=".gmdate("M d Y H:i:s", time()+(60*60*((1/60)*COOKIE_LIFETIME)))." GMT; path=".COOKIE_PATH."; domain=".COOKIE_DOMAIN.";");
- if(!setcookie($this->sess_name,$id,time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH,COOKIE_DOMAIN)) echo('<b>Could not send cookies to browser</b>');
- if (PHPVERSION<4.10) $GLOBALS['HTTP_COOKIE_VARS'] = array($this->sess_name=>$id);
- else $GLOBALS['_COOKIE'] = array($this->sess_name=>$id);
- //setcookie($this->sess_name,$this->_generate_id(),time()+(60*60*((1/60)*COOKIE_LIFETIME)),COOKIE_PATH,COOKIE_DOMAIN);
- }
- } else {
- //header("Set-Cookie: {$this->sess_name}={$id}; expires=".gmdate("M d Y H:i:s", time()+(60*60*((1/60)*COOKIE_LIFETIME)))." GMT; path=".COOKIE_PATH);
- if(!setcookie($this->sess_name,$id,time()+((defined('COOKIE_LIFETIME')?(60*60*((1/60)*COOKIE_LIFETIME)):3600)),COOKIE_PATH)) echo('<b>Could not send cookies to browser</b>');
- if (PHPVERSION<4.10) $GLOBALS['HTTP_COOKIE_VARS'] = array($this->sess_name=>$id);
- else $GLOBALS['_COOKIE'] = array($this->sess_name=>$id);//D, d-M-Y H:i:s
- //if(!setcookie($this->sess_name,$this->_generate_id(),time()+(60*60*((1/60)*COOKIE_LIFETIME)),COOKIE_PATH)) die('Web Browser cannot accept cookie');
- }
- if($this->type==SESS_TYPE_FILE) {
- /*$fid = fopen($this->directory."/sess_".$this->id,"w");
- if(!$fid) return false;
- fclose($fid);*/
- if(file_exists($this->sess_file)) unlink($this->sess_file);
- $this->sess_file = $this->directory."/sess_".$this->id;
- }
- return true;
- }
-
- function _getCookie($name) {
- if (PHPVERSION<4.10) {
- if(isset($GLOBALS['HTTP_COOKIE_VARS'][$name])) return $GLOBALS['HTTP_COOKIE_VARS'][$name]; else return;
- } else {
- if(isset($GLOBALS['_COOKIE'][$name])) return $GLOBALS['_COOKIE'][$name]; else return;
- }
- }
-
- function _getRegisteredSessions() {
- if($this->type==SESS_TYPE_FILE&&file_exists($this->sess_file)) {
- return unserialize(file_get_contents($this->sess_file));
- } else if($this->type==SESS_TYPE_PHP) {
- return (isset($GLOBALS[$this->_varname]))?$GLOBALS[$this->_varname]:"";
- }
- }
-
- function _generate_id() {
- $this->id = md5(microtime().getenv('HTTP_ACCEPT_CHARSET').getenv('HTTP_ACCEPT_ENCODING').getenv('HTTP_ACCEPT_LANGUAGE').getenv('REMOTE_ADDR').getenv('HTTP_USER_AGENT').getenv('REMOTE_HOST').getenv('REMOTE_PORT'));
- return $this->id;
- }
- function _write($filename) {
- if($this->type==SESS_TYPE_FILE) {
- if(file_exists($filename)) $data = unserialize(file_get_contents($filename));
- }
- if(!isset($GLOBALS[$this->_varname])||!is_array($GLOBALS[$this->_varname])) {
- if (PHPVERSION<4.01) {
- echo '<b>WARNING:</b> Wrong type of session variables or array variables needed';
- }
- else if (PHPVERSION>4.01) {
- trigger_error('Wrong type of session variables or array variables needed',E_USER_WARNING);
- }
- }
- if(isset($data)) {
- while(list($k,$v)=each($GLOBALS[$this->_varname])) {
- $data[$k] = $v;
- }
- }
- if(($this->type==SESS_TYPE_FILE)&&(!USE_PHP_DEFAULT_HANDLER)) {
- $fp = fopen($filename,"w");
- if($fp) {
- fwrite($fp,serialize($GLOBALS[$this->_varname]));
- fclose($fp);
- } else {
- if (PHPVERSION<4.01) {
- die('Unable to write onto session file, write permission denied');
- }
- else if (PHPVERSION>4.01) {
- trigger_error('Unable to write onto session file, write permission denied',E_USER_ERROR);
- }
- }
- }
- }
-
- }
-
- ?>