Write session to Database

Source: Internet
Author: User
Tags getmessage

Use the Session_set_save_handler () function to write the contents of the session to the database

1<?PHP2     /*3 * @author Fahy4 * @link http://home.cnblogs.com/u/HuangWj5 * database for MySQL,6 * Database name is session, table name is session,7 * The fields in the table include Phpsessid,update_time,client_ip,data8     */9     classsession{Ten         Private Static $handler=NULL; One         Private Static $ip=NULL; A         Private Static $lifetime=NULL; -         Private Static $time=NULL; -          the         //Configuring static Variables -         Private Static functionInit$handler){ -Self::$handler=$handler;//Get Database Resources -Self::$ip= !Empty($_server["REMOTE_ADDR"])?$_server["REMOTE_ADDR"]: ' unkonw ';//Get client IP +Self::$lifetime=Ini_get(' Session.gc_maxlifetime ');//Get session life cycle -Self::$time= Time();//Get current Time +         } A         //Call the Session_set_save_handler () function and open the session at         Static functionStart$pdo){ -Self::init ($pdo); -             Session_set_save_handler( -                 Array(__class__, ' open '), -                 Array(__class__, ' close '), -                 Array(__class__, ' read '), in                 Array(__class__, ' write '), -                 Array(__class__, ' Destroy '), to                 Array(__class__, ' GC ') +             ); -             Session_Start(); the         } *          $          Public Static functionOpen$path,$name){Panax Notoginseng             return true; -         } the          Public Static functionClose () { +             return true; A         } the          +         //querying data in a database -          Public Static functionRead$PHPSESSID){ $              $sql= "Select Phpsessid,update_time,client_ip,data from session where phpsessid=?"; $              $stmt= Self::$handler->prepare ($sql); -              $stmt->execute (Array($PHPSESSID)); -              if(!$result=$stmt->fetch (PDO::Fetch_assoc)) { the                  return‘‘; -              }Wuyi              if(Self::$ip==$result[' Client_ip ']){ theSelf::d Estroy ($PHPSESSID); -                  return‘‘; Wu              } -              if(($result[' Update_time ']+self::$lifetime) <self::$time){ AboutSelf::d Estroy ($PHPSESSID); $                  return‘‘; -              } -              return $result[' Data ']; -         } A         /* + * First query the session whether there is data, if present, update the data, if not, insert the data the          */ -         //writes the session to the database, $data the keys and values array in the session $          Public Static functionWrite$PHPSESSID,$data){ the             $sql= "Select Phpsessid,update_time,client_ip,data from session where phpsessid=?"; the              $stmt= Self::$handler->prepare ($sql); the              $stmt->execute (Array($PHPSESSID)); the               -              if($result=$stmt->fetch (PDO::Fetch_assoc)) {                 in                  if($result[' data ']! =$data|| Self::$time> ($result[' Update_time ']+30)){ the                      $sql= "Update session set Update_time=?,data=?" where PHPSESSID =? "; the                      $stmt= Self::$handler->prepare ($sql); About                      $stmt->execute (Array($self::$time,$data,$PHPSESSID)); the                 } the}Else{ the                  if(!Empty($data)){ +                      Try{ -                          $sql= "INSERT into session (Phpsessid,update_time,client_ip,data) VALUES (?,?,?,?)"; the}Catch(pdoexception$e){Bayi                          Echo $e-getMessage (); the                      } the                      $sth= Self::$handler->prepare ($sql); -                      $sth->execute (Array($PHPSESSID, Self::$time, Self::$ip,$data)); -                  } the              } the              return true; the         } the          -          Public Static functionDestroy$PHPSESSID){ the             $sql= "Delete from session where PHPSESSID =?"; the             $stmt= Self::$handler->prepare ($sql); the             $stmt->execute (Array($PHPSESSID));94             return true; the         } the          Public Static functiongc$lifetime){ the             $sql= "Delete from session where update_time<?";98             $stmt= Self::$handler->prepare ($sql); About             $stmt->execute (Array(Self::$time-$lifetime)); -             return true;101         }102     }103     //connect to a database using PDO104     Try{ the         $pdo=NewPDO ("Mysql:host=localhost;dbname=session", "root", "hwj193");106}Catch(pdoexception$e){107         Echo $e-getMessage ();108     }109     //Passing Database Resources theSession::start ($pdo);

Write session to Database

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.