Database store session Information code, store session Code _php Tutorial

Source: Internet
Author: User

Database store session Information code, store session code


Today to everyone on a piece of code, the database stores session information , you just need to put the following code in the session file, and then Session_Start () to introduce the Sessiong file on the line, Of course you don't have to write Session_Start ().

This is the structure of the database table

Okay, here's the code.

Class Session {
private static $_mysqli;

public static function action () {
Ini_set (' Session.save_handler ', ' user ');
Session_set_save_handler (Array (__class__, ' open '),
Array (__class__, ' close '),
Array (__class__, ' read '),
Array (__class__, ' write '),
Array (__class__, ' destroy '),
Array (__class__, ' GC '));
@session_start ();
}

public static function open ($path, $name) {
Self::$_mysqli = new mysqli (' localhost ', ' root ', ' ', ' test ');
return true;
}

public static function close () {
Self::$_mysqli->close ();
return true;
}

public static function read ($SID) {
$_query = "Select Sdata from session WHERE sid= ' {$sid} ' LIMIT 1";
$_result = Self::$_mysqli->query ($_query);
$_sdata = $_result->fetch_object ();
Return $_sdata->sdata;
}

public static function Write ($sid, $sdata) {
$_query = "Select Sid from Session WHERE sid= ' {$sid} ' LIMIT 1";
$_result = Self::$_mysqli->query ($_query);
if (!! $_sid = $_result->fetch_object ()) {
$_query = "UPDATE session SET sdata= ' {$sdata} ' WHERE sid= ' {$sid} '";
Self::$_mysqli->query ($_query);
} else {
$_query = "INSERT into session (Sid,sdata) VALUES (' {$sid} ', ' {$sdata} ')";
Self::$_mysqli->query ($_query);
}
return true;
}

public static function Destroy ($SID) {
$_query = "DELETE from session WHERE sid= ' {$sid} ' LIMIT 1";
Self::$_mysqli->query ($_query);
Setcookie (Ini_get (' Session.name '), ", Time ()-1);
return true;
}

public static function GC ($maxlifetime) {
$_query = "DELETE from session WHERE now ()-slasttime> ' {$maxlifetime} '";
Self::$_mysqli->query ($_query);
return true;
}
}

Session::action ();

http://www.bkjia.com/PHPjc/1014828.html www.bkjia.com true http://www.bkjia.com/PHPjc/1014828.html techarticle Database storage Session information code, stored session code today to everyone on a piece of code, database storage session information, you just need to put the following code in the session file, then ...

  • 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.