This article mainly introduces the php session-based database interaction class. the instance analyzes php's skills related to database operations in combination with session, which has some reference value, for more information about php session-based database interaction, see the following example. Share it with you for your reference. The details are as follows:
<? Php/*** session database storage class */class Session {private static $ session_id = 0; private static $ session_data = array (); private static $ is_update = FALSE; private static $ is_del = FALSE; private static $ is_gc = FALSE; private static $ dbo = NULL; // database connection handle private static $ gc_max_time = 1440; private static $ table = 'session'; private static $ pre_key = 'weige '; // session key // bind with haprivate static $ gc_rate _ De = 100; // indicates the denominator private static $ gc_rate_co = 20; // indicates the numerator private static $ path = '/'; // The storage path private static $ domain = null; // domain private static $ secure = false; // Default private static $ httponly = false; // Default/*** obtain the private database handle */private static function open () {if (! Self ::$ dbo) {self ::$ dbo = Db: factory ();} return TRUE ;} /*** set **/public static function set ($ key, $ val = NULL) {self: open (); $ data = self: read (); if ($ data = FALSE) {$ data = array ();} if (! $ Val & is_array ($ key) {$ data = $ key;} else if ($ val & is_string ($ key )) {$ data [$ key] = $ val;} self: write ($ data); self: close ();} /*** get value **/public static function get ($ key = NULL) {self: open (); self: $ session_data = self: read (); $ ret = ''; if (! $ Key) {$ ret = self: $ session_data;} else if (is_array (self: $ session_data) & isset (self: $ session_data [$ key]) {$ ret = self ::$ session_data [$ key];} self: update (); self: close (); return $ ret ;} /*** delete or reset **/public static function del ($ key) {if (! Self ::$ is_del) {self: open (); $ val = self: read (); if (isset ($ val [$ key]) {unset ($ val [$ key]);} $ session_id = self ::$ session_id; $ session_data = serialize ($ val); $ session_expire = TIME + self :: get_gc_maxtime (); self: $ dbo-> query ("update ". self: $ table. "set value = '$ session_data', expiry = '$ session_expire' where session_id = '$ session_id'"); self: close ();} self :: $ is_del = TRUE;}/*** destroy ***/pu Blic static function destroy () {$ session_id = self: get_session_id (); $ _ COOKIE ['wbsid '] = ''; self: open (); self :: $ dbo-> query ("delete from ". self: $ table. "where session_id = '$ session_id'"); self: close ();}/*** read private **/private static function read () {$ session_id = self:: $ session_id; if (! $ Session_id) {$ session_id = self: get_session_id ();} if (! $ Session_id) return array (); $ user_agent = isset ($ _ SERVER ['http _ USER_AGENT '])? Md5 ($ _ SERVER ['http _ USER_AGENT ']): ''; $ client_ip = Fun: getIp (); $ session_expire = TIME-self: get_gc_maxtime (); $ rs = self: $ dbo-> fetchRow ("select session_id, value, agent, ip from ". self: $ table. "where session_id = '$ session_id' and expiry> '$ session_expire'"); if (! $ Rs | $ rs ['agent']! = $ User_agent | $ rs ['IP']! = $ Client_ip) {return FALSE;} self: $ session_id = $ rs ['session _ id']; return unserialize ($ rs ['value']);} /*** session write private **/private static function write (array $ session_data) {$ session_id = self: $ session_id; if (! $ Session_id) {$ session_id = self: get_session_id () ;}$ session_expire = TIME + self: get_gc_maxtime (); $ user_agent = isset ($ _ SERVER ['http _ USER_AGENT '])? Md5 ($ _ SERVER ['http _ USER_AGENT ']): ''; $ client_ip = Fun: getIp (); $ session_data = serialize ($ session_data); if (self:: $ session_id & self: $ session_id ==$ session_id) {self ::$ dbo-> query ("update ". self: $ table. "set value = '$ session_data', expiry = '$ session_expire', agent = '$ user_agent', ip = '$ client_ip' where session_id = '$ session_id '");} else {self ::$ session_id = $ session_id = self: create_session _ Id (); self: $ dbo-> query ("insert ". self: $ table. "(session_id, value, expiry, agent, ip) values ('$ session_id', '$ session_data', '$ session_expire', '$ user_agent', '$ client_ip ') ");} return true;}/*** session update private **/private static function update () {if (! Self: $ is_update) {$ session_id = self ::$ session_id; $ session_expire = TIME + self: get_gc_maxtime (); self :: $ dbo-> query ("update ". self: $ table. "set expiry = '$ session_expire' where session_id = '$ session_id'");} self: $ is_update = TRUE;} private static function close () {if (! Self: $ is_gc & mt_rand (1, self: $ gc_rate_de) % self: $ gc_rate_co = 0) {self: gc ();} self :: $ is_gc = TRUE;}/*** randomly triggered by clearing expired sessions **/private static function gc () {$ session_expire = TIME-self: get_gc_maxtime (); self :: $ dbo-> query ("delete from ". self: $ table. "where expiry <'$ session_expire'");} private static function get_session_id () {if (isset ($ _ COOKIE ['wbsid ']) & strlen ($ _ COOKIE ['wbsid']) = 32) {$ sid = $ _ COOKIE ['wbsid ']; setcookie ('wbsid', $ sid, TIME + self: get_gc_maxtime (), self :: $ path, self ::$ domain, self ::$ secure, self ::$ httponly); return $ sid;} return null;} private static function create_session_id () {$ sid = self: get_session_id (); if (! $ Sid) {$ sid = Fun: getIp (). TIME. microtime (TRUE ). mt_rand (0,100), mt_rand (100000,900 00000); $ sid = md5 (self: $ pre_key. $ sid); setcookie ('wbsid', substr ($ sid, 0, 32), TIME + self: get_gc_maxtime (), self: $ path, self :: $ domain, self ::$ secure, self ::$ httponly);} return $ sid;} public static function get_gc_maxtime () {return self ::$ gc_max_time ;}}
I hope this article will help you with php programming.