《php独立session数据库存储操作类分享_.docx》由会员分享,可在线阅读,更多相关《php独立session数据库存储操作类分享_.docx(21页珍藏版)》请在三一文库上搜索。
1、php独立session数据库存储操作类分享_ 挺直上代码: 代码如下: class dbsession const type_int = 1; const type_str = 2; /* * database configration * * var array */ private $_config = array( host = 127.0.0.1, port = 3306, username = root, password = root, dbname = db_mylab, tablename = t_sessions, cookie_prefix = mylab_, cooki
2、epath = /, cookiedomain = ”, cookie_timeout = 900 ); /* * table fields type array * * var array */ private $_db_fields = array( crc32sid = self:type_int, sessionhash = self:type_str, idhash = self:type_str, userid = self:type_int, ipaddress = self:type_str, lastactivity = self:type_str, location = s
3、elf:type_str, loggedin = self:type_int, heartbeat = self:type_str ); /* * db obj * * var mysqli object */ private $_mysqli = null; /* * weather the session was created or existed previously * * var bool */ private $_created = false; /* * array of changes. * * var array */ private $_changes = array()
4、; /* * var bool */ private $_db_inited = false; /* * session host * * var string */ private $_session_host = ”; /* * session idhash * * var string */ private $_session_idhash = ”; private $_dbsessionhash = ”; private $_vars = array(); public function _construct() $this-_dbsessionhash = addslashes($t
5、his-get_cookie(sessionhash); $this-_session_host = substr($_serverremote_addr, 0, 15); #this should *never* change during a session $this-_session_idhash = md5($_serverhttp_user_agent . self:fetch_substr_ip(self:fetch_alt_ip() ); $this-_init_config(); $this-init_db(); $gotsession = false; if ($this-
6、_dbsessionhash) $sql = select * from . $this-_configtablename . where crc32sid = . sprintf(%u, crc32($this-_dbsessionhash) . and sessionhash = . $this-_dbsessionhash . and idhash = . $this-_session_idhash . and heartbeat . date(y-m-d h:i:s ,timenow $this-_configcookie_timeout) . ; /echo $sql;exit; $
7、result = $this-_mysqli-query($sql); $session = $result-fetch_array(mysqli_assoc); if ($session and ($this-fetch_substr_ip($sessionipaddress) = $this-fetch_substr_ip($this-_session_host) $gotsession = true; $this-_vars = $session; $this-_created = false; if ($gotsession = false) $this-_vars = $this-f
8、etch_session(); $this-_created = true; $gotsession = true; if ($this-_created = false) $this-set(lastactivity, date(y-m-d h:i:s, timenow); $this-set(location, $_serverrequest_uri); /* * builds an array that can be used to build a query to insert/update the session * * return array array of column na
9、me = prepared value */ private function _build_query_array() $return = array(); foreach ($this-_db_fields as $fieldname = $cleantype) switch ($cleantype) case self:type_int: $cleaned = is_numeric($this-_vars$fieldname) ? $this-_vars$fieldname : intval($this-_vars$fieldname); break; case self:type_st
10、r: default: $cleaned = . addslashes($this-_vars$fieldname) . ; $return$fieldname = $cleaned; return $return; /* * sets a session variable and updates the change list. * * param string name of session variable to update * param string value to update it with */ public function set($key, $value) if ($
11、this-_vars$key != $value) $this-_vars$key = $value; $this-_changes$key = true; public function get($key) return $this-_vars$key; public function unsetchangedvar($var) if (isset($this-_changes$var) unset($this-_changes$var); /* * fetches a valid sessionhash value, not necessarily the one tied to this
12、 session. * * return string 32-character sessionhash */ static function fetch_sessionhash() return hash(md5 , timenow . rand(1, 100000) . uniqid() ); private function _init_config() $registry = zend_registry:getinstance(); $config = $registry-get(config); $this-_confighost = $config-database-params-
13、host; $this-_configport = $config-database-params-port; $this-_configusername = $config-database-params-username; $this-_configpassword = $config-database-params-password; $this-_configdbname = $config-database-params-dbname; $this-_configtablename = $config-database-session-tablename; /* * initiali
14、ze database connection */ public function init_db() if ($this-_db_inited) return true; /mysqli_report(mysqli_report_off); $this-_mysqli = new mysqli( $this-_confighost, $this-_configusername, $this-_configpassword, $this-_configdbname, $this-_configport ); /* check connection */ if (mysqli_connect_e
15、rrno() / printf(connect failed: %sn, mysqli_connect_error(); / echo in , _file_, on line , _line_; echo success: false, errors: reason: connect failed: . addslashes( mysqli_connect_error() ) . ; exit(); $this-_mysqli-query(set names latin1); $this-_db_inited = true; return true; /* * fetches an alte
16、rnate ip address of the current visitor, attempting to detect proxies etc. * * return string */ static function fetch_alt_ip() $alt_ip = $_serverremote_addr; if (isset($_serverhttp_client_ip) $alt_ip = $_serverhttp_client_ip; else if (isset($_serverhttp_from) $alt_ip = $_serverhttp_from; return $alt
17、_ip; /* * returns the ip address with the specified number of octets removed * * param string ip address * * return string truncated ip address */ static function fetch_substr_ip($ip, $length = null) return implode(., array_slice(explode(., $ip), 0, 4 $length); /* * fetches a default session. used w
18、hen creating a new session. * * param integer user id the session should be for * * return array array of session variables */ public function fetch_session($userid = 0) $sessionhash = self:fetch_sessionhash(); $this-set_cookie(sessionhash, $sessionhash); return array( crc32sid = sprintf(%u, crc32($
19、sessionhash), sessionhash = $sessionhash, idhash = $this-_session_idhash, userid = $userid, ipaddress = $this-_session_host, lastactivity = date(y-m-d h:i:s, timenow), location = $_serverrequest_uri, loggedin = $userid ? 1 : 0, heartbeat = date(y-m-d h:i:s, timenow) ); public function get_cookie($co
20、okiename) $full_cookiename = $this-_configcookie_prefix . $cookiename; if (isset($_cookie$full_cookiename) return $_cookie$full_cookiename; else return false; public function set_cookie($name, $value = ”, $permanent = 1, $allowsecure = true) if ($permanent) $expire = timenow + 60 * 60 * 24 * 365; el
21、se $expire = 0; if ($_serverserver_port = 443) / were using ssl $secure = 1; else $secure = 0; / check for ssl $secure = (req_protocol = https and $allowsecure) ? true : false); $name = $this-_configcookie_prefix . $name; $filename = n/a; $linenum = 0; if (!headers_sent($filename, $linenum) / consid
22、er showing an error message if there not sent using above variables? if ($value = ” and strlen($this-_configcookiepath) 1 and strpos($this-_configcookiepath, /) != false) / this will attempt to unset the cookie at each directory up the path. / ie, cookiepath = /test/abc/. these will be unset: /, /te
23、st, /test/, /test/abc, /test/abc/ / this should hopefully prevent cookie conflicts when the cookie path is changed. $dirarray = explode(/, preg_replace(#/+$#, ”, $this-_configcookiepath); $alldirs = ”; foreach ($dirarray as $thisdir) $alldirs .= $thisdir; if (!empty($thisdir) / try unsetting without
24、 the / at the end setcookie($name, $value, $expire, $alldirs, $this-_configcookiedomain, $secure); $alldirs .= /; setcookie($name, $value, $expire, $alldirs, $this-_configcookiedomain, $secure); else setcookie($name, $value, $expire, $this-_configcookiepath, $this-_configcookiedomain, $secure); else
25、 if (!debug) echo cant set cookies; private function _save() $cleaned = $this-_build_query_array(); if ($this-_created) /var_dump($cleaned); # insert query $this-_mysqli-query( insert ignore into . $this-_configtablename . ( . implode(, array_keys($cleaned) . ) values ( . implode(, $cleaned). ) ); e
26、lse # update query $update = array(); foreach ($cleaned as $key = $value) if (!empty($this-_changes$key) $update = $key = $value; if (sizeof($update) 0) $sql = update . $this-_configtablename . set . implode(, , $update) . where crc32sid = . sprintf(%u, crc32($this-_dbsessionhash) . and sessionhash
27、= . $this-_dbsessionhash . ; /echo $sql; $this-_mysqli-query($sql); public function getonlineusernum() $sql = select count(*) as cnt from . $this-_configtablename . where loggedin = 1 and heartbeat . date(y-m-d h:i:s ,timenow $this-_configcookie_timeout) . ; $result = $this-_mysqli-query($sql); $row
28、 = $result-fetch_array(mysqli_assoc); return $rowcnt; private function _gc() $rand_num = rand(); # randow integer between 0 and getrandmax() if ($rand_num 100) $sql = delete from . $this-_configtablename . where heartbeat . date(y-m-d h:i:s ,timenow $this-_configcookie_timeout) . ; $this-_mysqli-query($sql); public function _destruct() $this-_save(); $this-_gc(); $this-_mysqli-close(); 更多信息请查看IT技术专栏 .