Session: Share the php class stored in the database

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
Class SessionToDB
{
Private $ _ path = null;
Private $ _ name = null;
Private $ _ pdo = null;
Private $ _ ip = null;
Private $ _ maxLifeTime = 0;

Public function _ construct (PDO $ pdo)
{
Session_set_save_handler (
Array (& $ this, 'open '),
Array (& $ this, 'close '),
Array (& $ this, 'read '),
Array (& $ this, 'write '),
Array (& $ this, 'deststroy '),
Array (& $ this, 'gc ')
);

$ This-> _ pdo = $ pdo;
$ This-> _ ip =! Empty ($ _ SERVER ['remote _ ADDR '])? $ _ SERVER ['remote _ ADDR ']: null;
$ This-> _ maxLifeTime = ini_get ('session. gc_maxlifetime ');
}

Public function open ($ path, $ name)
{
Return true;
}

Public function close ()
{
Return true;
}

Public function read ($ id)
{
$ SQL = 'select * FROM session where PHPSESSID =? ';
$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array ($ id ));

If (! $ Result = $ stmt-> fetch (PDO: FETCH_ASSOC )){
Return null;
} Elseif ($ this-> _ ip! = $ Result ['client _ ip']) {
Return null;
} Elseif ($ result ['Update _ time'] + $ this-> _ maxLifeTime <time ()){
$ This-> destroy ($ id );
Return null;
} Else {
Return $ result ['data'];
}
}

Public function write ($ id, $ data)
{
$ SQL = 'select * FROM session where PHPSESSID =? ';
$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array ($ id ));

If ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC )){
If ($ result ['data']! = $ Data ){
$ SQL = 'Update session SET update_time =? , Date =? Where phpsessid =? ';

$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array (time (), $ data, $ id ));
}
} Else {
If (! Empty ($ data )){
$ SQL = 'insert INTO session (PHPSESSID, update_time, client_ip, data) VALUES (?,?,?,?) ';
$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array ($ id, time (), $ this-> _ ip, $ data ));
}
}

Return true;
}

Public function destroy ($ id)
{
$ SQL = 'delete FROM session WHERE PHPSESSID =? ';
$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array ($ id ));

Return true;
}

Public function gc ($ maxLifeTime)
{
$ SQL = 'delete FROM session WHERE update_time <? ';
$ Stmt = $ this-> _ pdo-> prepare ($ SQL );
$ Stmt-> execute (array (time ()-$ maxLifeTime ));

Return true;
}
}

Try {
$ Pdo = new PDO ('mysql: host = localhost; dbname = rphp4zf ', 'root', 'rickyfeng ');
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );

New SessionToDB ($ pdo );
} Catch (PDOException $ e ){
Echo 'error: '. $ e-> getMessage ();
}

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.