PHP encapsulated Database Save session function class _php tips

Source: Internet
Author: User
Tags php class php database

This example describes the PHP encapsulated database save session function class. Share to everyone for your reference, specific as follows:

PHP saves the session class with the database:

<?php class Safesessionhandler implements Sessionhandlerinterface {public $save _path;
  Public $session _name;
  Public $table;
  Public Function __construct () {$this->table = new Table ("Safe_session");
    Private Function Session_id_parse ($session _id) {$time = Hexdec (substr ($session _id, 0, 8));
    $skey = substr ($session _id, 8);
  Return Array ($time, $skey);
    Public function Close () {Loginfo (' close: ');
  return true;
    The Public Function Create_sid () {Loginfo ("Create_sid:");
    $time = time ();
    $skey = "";
    $char = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    For ($i =0 $i <52; $i + +) {$skey. = $char {Mt_rand (0, 61)};
    $session = Array ("Time" => $time, "Skey" => $skey, "Sval" => "",);
    $this->table->insert ($session); Return Dechex ($time).
  $skey;
    The Public function destroy ($session _id) {loginfo ("Destroy:%s", $session _id); List ($time, $skey) = $this->session_id_parse ($session _id);
    $this->table->where ("time =?", $time)->where ("Skey =?", $skey)->delete ();
  return true;
    The Public Function gc ($maxlifetime) {loginfo ("GC:%s", $maxlifetime);
    $this->table->where ("Time <.", Time ()-86400 *)->delete ();
  return true;
    Public function open ($save _path, $session _name) {loginfo ("Open:%s,%s", $save _path, $session _name);
    $this->save_path = $save _path;
    $this->session_name = $session _name;
  return true;
    The public function read ($session _id) {loginfo ("read:%s", $session _id);
    List ($time, $skey) = $this->session_id_parse ($session _id);
    $row = $this->table->where ("time =?", $time)->where ("Skey =?", $skey)->select ()->fetch ();
    if (empty ($row)) {return "";
  return $row ["Sval"];
    Public Function Write ($session _id, $session _data) {loginfo ("write:%s,%s", $session _id, $session _data); $session = Array ("Sval" => $session _data);
    List ($time, $skey) = $this->session_id_parse ($session _id);
    $this->table->where ("time =?", $time)->where ("Skey =?", $skey)->update ($session);
  return true;

 }
}

For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic grammar Introductory Course" , "PHP Operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "Php+mysql database operation Introductory Course" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

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.