PHP custom session data storage, PHP session data stored in MYSQL

Source: Internet
Author: User
Tags php session
The following is an example of saving session data to mysql in PHP. put all the files in the same directory.

PHP custom session data storage, PHP session data stored in MYSQL

The following is an example of saving session data to mysql in PHP. put all the files in the same directory.

1. config. ini file content

Dbhost = 127.0.0.1
Dbuser = root
Dbpassword = root
Dbname = sessionmysql
Dbcharset = utf8config. class. php

2. config. class. php

 

 

 * @ Version $ Id: BookManage. php, v 1.0 2010/05/13 21:23:55 uw Exp * @ copyright©2010, xudianyang **/class Config {public $ dbhost; // database server address public $ dbuser; // database username public $ dbpass; // database password public $ dbname; // database name public $ dbcharset; // database code public function _ construct () {$ fp = fopen ("config. ini "," rb "); // read the configuration file $ arr = array (); $ str =" "; while (! Feof ($ fp) {$ str = trim (fgets ($ fp); if ($ str) {$ arrsub = explode ("=", $ str ); $ arr [$ arrsub [0] = trim ($ arrsub [1]) ;}} fclose ($ fp ); $ this-> dbhost = $ arr ["dbhost"]; $ this-> dbuser = $ arr ['dbuser']; $ this-> dbpass = $ arr ['dbpassword']; $ this-> dbname = $ arr ["dbname"]; $ this-> dbcharset = $ arr ['dbcharset'] ;}}$ config = new Config ();?
 
3. sessionmysql. class. php
 
 Dbhost; self ::$ dbuser = $ config-> dbuser; self ::$ dbpass = $ config-> dbpass; self ::$ dbcharset = $ config-> dbcharset; self:: $ dbname = $ config-> dbname; session_module_name ("user"); values ("SessionMysql: Role", "SessionMysql: mysql_session_close", "SessionMysql: mysql_session_read ", "SessionMysql: mysql_session_write", "SessionMysql: mysql_session_destroy", "SessionMysql: mysql_session_gc ");} Public static function mysql_session_open ($ session_save_path, $ session_name) {// establish a connection with mysql self ::$ link = mysql_connect (self :$ dbhost, self ::$ dbuser, self:: $ dbpass) or die ("failed to establish connection with the database server, probably because the server is not enabled ". mysql_erro); mysql_select_db ("sessionmysql", self: $ link); mysql_query ("set names utf8", self: $ link); return true ;} public static function mysql_session_close () {mysql_close (self ::$ link);} public static function mys Ql_session_read ($ sid) {$ SQL = "select * from 'session' where 'Sid '=' $ sid 'and 'expire'> ". time (); $ res = mysql_query ($ SQL, self: $ link); if (mysql_num_rows ($ res) {$ row = mysql_fetch_assoc ($ res ); return $ row ['value'];} else {return "" ;}} public static function mysql_session_write ($ sid, $ value) {$ lifetime = get_cfg_var ("session. gc_maxlifetime "); $ expiretime = date (" Y-m-d H: I: s ", time () + $ lifetime); $ SQL =" insert into 'sessio Ns 'values ('$ sid', '$ expiretime', '$ value') "; $ res = mysql_query ($ SQL, self: $ link); if (! $ Res) {$ SQL = "update 'session' set 'expire '=' $ expiretime ', 'value' = '$ value' where 'Sid' =' $ Sid' and 'expire '> ". time (); mysql_query ($ SQL, self: $ link); return true;} public static function mysql_session_destroy ($ sid) {$ SQL = "delete from 'session' where 'Sid '=' $ sid '"; $ res = mysql_query ($ SQL, self: $ link ); return $ res;} public static function mysql_session_gc ($ lifetime) {$ SQL = "delete from 'session' wh Ere ('expire '-". time (). ") <0"; $ res = mysql_query ($ SQL, self: $ link); return mysql_affected_rows ($ res) ;}} new SessionMysql ($ config);?>
4. ceshi. php
 
 
5. ceshi2.php
 
 
 
6. sessionmysql. SQL database file (please create a new sessionmysql database and then execute the following SQL statement)
 
-- PhpMyAdmin SQL Dump -- version 3.1.3.1 -- http://www.phpmyadmin.net ---- host: localhost -- generation date: May 02, 2011 -- Server version: 5.1.45 -- PHP version: 5.3.1SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";/*! 40101 SET @ OLD_CHARACTER_SET_CLIENT = @ CHARACTER_SET_CLIENT */;/*! 40101 SET @ OLD_CHARACTER_SET_RESULTS = @ CHARACTER_SET_RESULTS */;/*! 40101 SET @ OLD_COLLATION_CONNECTION = @ COLLATION_CONNECTION */;/*! 40101 set names utf8 */; ---- database: 'sessionmysql' ---- orders TABLE structure 'session' -- create table if not exists 'Session' ('Sid 'varchar (32) not null comment 'session. ID', 'expire 'datetime not null comment 'session. gc_maxlifetime', 'value' text not null, primary key ('Sid ') ENGINE = MyISAM default charset = utf8 COMMENT = 'save session table ';
 

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.