PHP class Instance _php technique based on session to implement database interaction

Source: Internet
Author: User

The example in this article describes the class that PHP implements database interaction based on session. Share to everyone for your reference. 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 the HA private static $GC _rate_de = 100;//represents the denominator private static $GC _ra Te_co = 20;//representing 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 Stati
  C function 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->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->query ("Delete from". Self:: $table. "Where session_id= ' $session _id '");
 Self::close ();
  /** * Read private */privacy 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 * * */privacy 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 into". Self:: $table. " (session_id, value, expiry, agent, IP) VALUES (' $session _id ', ' $session _data ', ' $session _expire ', ' $user _agent ', ' $cli
  Ent_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;
   /** * Expired session purge random trigger * */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 '))
   {$sid = $_cookie[' Wbsid '];
   Setcookie (' Wbsid ', $sid, Time + self::get_gc_maxtime (), Self:: $path, Self:: $domain, Self:: $secure, Self:: $httponly);
  return $sid;
 return null; The 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 will help you with your PHP programming.

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.