The database stores the session information code and the session code.

Source: Internet
Author: User

The database stores the session information code and the session code.

Today, I will give you a piece of code,Database stores session informationYou only need to put the following code in the session file, and then introduce sessiong to session_start (). Of course, you don't need to write session_start ().

This is the structure of the database table.

Okay. The following code is available:

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 __, 'deststroy '),
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 ();

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.