PHP class instance for database interaction based on session, session instance _php tutorial

Source: Internet
Author: User

PHP class instance for database interaction based on session, session instance


The examples in this paper are the classes that PHP implements for database interaction based on session. Share to everyone for your reference. Specific 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 = ' Sessions '; private static $pre _key = ' weige ';//session key//bundle uses HA private static $GC _rate_de = 100;//represents the denominator private static $GC _rate_ CO = 20;//represents the molecule private static $path = '/';//save path private static $domain = null; Domain private static $secure = false;//default private static $httponly = false;//default/** * Get database handle private */private static Func  tion Open () {if (!self:: $dbo) {self:: $dbo = Db::factory (); } return TRUE;  }/** * * */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 ();  }/** * Gets the 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 the->query ("Update". Self:: $table. "Set value= ' $session _data ', expiry= ' $session _expire ' where session_id= '   $session _id ' ");  Self::close (); } Self:: $is _del = TRUE;  }/** * Destroy * * */public static function Destroy () {$session _id = self::get_session_id (); $_cookie[' wbsid '] = ';  Self::open ();  Self:: $dbo the->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. "Se T 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 INTO '. Self:: $table. " (session_id, value, expiry, agent, IP) VALUES (' $session _id ', ' $session _data ', ' $session _expire ', ' $user _agent ', ' $clie  Nt_ip ') "); } return true; }/** * Session Update Private * */private static function update () {if (!self:: $is _update) {$session _id = self:: $sessio   n_id;   $session _expire = time + self::get_gc_maxtime (); Self:: $dbo the->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;   }/** * Expire session clear RANDOM trigger * */private static function gc () {$session _expire = Time-self::get_gc_maxtime (); Self:: $dbo the->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 (Mt_rand (0), Mt_rand (100000, 90000000));   $SID = MD5 (self:: $pre _key. $sid); Setcookie (' Wbsid ', substr ($sid, 0, (+), 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1042686.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042686.html techarticle PHP based on the session implementation of database interaction class instance, the session example of this paper describes the PHP session based on the implementation of database interaction classes. Share to everyone for your reference. Specific as follows: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.