Use the PDO method to save the Session to MySQL data

Source: Internet
Author: User

Class:

<?php/* use database to save session */class Dbhandler implements Sessionhandlerinterface {protected $DBH;p ublic function open ($ Save_path, $name) {try {$this->connect ($save _path, $name); return true;} catch (Pdoexception $e) {echo $e GetMessage (); return false;}} Public function Close () {return true;} Public function Destroy ($session _id) {$sth = $this->dbh->prepare ("DELETE from sessions WHERE session_id =?"); $sth->execute (Array ($session _id)); return true;} Public Function GC ($maxlifetime) {$sth = $this->dbh->prepare ("DELETE from sessions WHERE last_update <?"); $sth->execute (Array (Time ()-$maxlifetime)); return true;}  Public function read ($session _id) {$sth = $this->dbh->prepare ("Select Session_data from sessions WHERE session_id = ?"); $sth->execute (Array ($session _id)), $row = $sth->fetch (pdo::fetch_num), if (count ($row) = = 0) {return ';} else { return $row [0];}} Public Function Write ($session _id, $session _data) {date_default_timezone_set (' PRC '); $now = time (); $sth = $This->dbh->prepare ("UPDATE sessions SET session_data =?, Last_update =?") WHERE session_id =? "); $sth->execute (Array ($session _data, $now, $session _id)), if ($sth->rowcount () = = 0) {$sth 2 = $this->dbh-> Prepare ("INSERT into sessions (session_id, Session_data, last_update) VALUES (?,?,?)"); $sth 2->execute (Array ($session _id, $session _data, $now));}} Public Function createtable ($save _path, $name, $connect = True) {if ($connect) {$this->connect ($save _path, $name);} $sql =<<<_sql_create TABLE Sessions (session_id VARCHAR) not null,session_data Mediumtext not null,last_ Update int not null,primary KEY (session_id)) _sql_; $this->dbh->exec ($sql);} Public function Connect ($save _path) {$parts = Parse_url ($save _path);p arse_str ($parts [' query '], $query);//$ DSN format: mysql:host=localhost;dbname=test$dsn = $parts [' scheme ']. ": host=". $parts [' Host ']. "; Dbname= ". $query [' dbname ']; $user = $query [' user ']; $password = $query [' password ']; $this->dbh = new PDO ($DSN, $user, $ password);$this->dbh->setattribute (Pdo::attr_errmode, pdo::errmode_exception); try {$this->dbh->query (' SELECT 1 From sessions LIMIT 1 ');} catch (Exception $e) {$this->createtable ($save _path, NULL, False);}}}

  

Use:

<?phpinclude './db.php '; Ini_set (' Session.save_path ', "mysql://localhost?user=root&password=&dbname= Test "); Ini_set (' Session.gc_maxlifetime ', 5); Ini_set (' Session.gc_divisor ', 2); Session_set_save_handler (new Dbhandler); Session_Start (), if (! isset ($_session[' visits ')) {$_session[' visits '] = 0;} $_session[' visits ']++;echo ' you are the first '. $_session[' visits ']. ' Second visit ';

 

Reference:

<php Cookbook>3 ' Rd

Use the PDO method to save the Session to MySQL data

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.