ThinkPHP implements the method of saving SESSION to MYSQL. thinkphpmysql_PHP tutorial

Source: Internet
Author: User
Tags php session
ThinkPHP implements the method of storing sessions into MYSQL, thinkphpmysql. ThinkPHP implements the method of saving sessions to MYSQL. thinkphpmysql explains ThinkPHP's method of saving sessions to MYSQL through examples, the running environment is ThinkPHP3.1.2. first, ThinkPHP implements the method of saving SESSION to MYSQL. thinkphpmysql

This article explains how ThinkPHP saves sessions to MYSQL through examples. the runtime environment used is ThinkPHP3.1.2.

First, set index. php:

<? Phpdefine ('app _ debug', true); // Set it to DEBUG mode require '.. /ThinkPHP. php '; // Set the ini_set ("session. save_handler "," user "); // Set the php session to be defined by the user

Set config. php:

<? Phpreturn array (// 'config map '=> 'configuration value' // add the database configuration letter 'show _ PAGE_TRACE' => true, 'DB _ type' => 'mysql ', // Database type 'DB _ host' => 'localhost', // server address 'DB _ name' => 'thinkphp ', // database name 'DB _ user' => 'Your username', // username 'DB _ pwd' => 'Your password ', // password 'DB _ port' => 3306, // PORT 'DB _ prefix' => 'think _', // The database table prefix is 'session _ options' => array ('type' => 'DB', // The SESSION uses the database to save 'expire '=> 1440, // The session expiration time. if not set, php is used. default value set in ini), 'session _ TABLE '=> 'think _ session', // This must be set. if the data TABLE cannot be found without a prefix, note that);?>

The database settings use DDL in SessionDb. class. php, but ENGINE = MyISAM default charset = utf8 is added later.

CREATE TABLE think_session (    session_id varchar(255) NOT NULL,    session_expire int(11) NOT NULL,    session_data blob,    UNIQUE KEY `session_id` (`session_id`)  )ENGINE=MyISAM DEFAULT CHARSET=utf8;

Now visit your index. php and find the think_session table in phpmyadmin. we will be pleasantly surprised to find that there are more data records.
Now the problem is fixed. Otherwise, SessionDb. class. php will be automatically loaded.

In this way, ThinkPHP calls

session('session_name','session_value')

The system automatically stores the session in the database created above.


How to store sessions in the database

To create a table structure for databases and databases, we can use any database that php can use. because php and mysql have the best combination, I will use mysql as an example, of course, you can change the name of another database based on your needs. at the same time, because mysql has no transaction function, it is faster than other databases. However, you can save the session book and never process things, in addition, I decided better.
Create a database:
The code is as follows: create database 'session '; create table 'session' (id CHAR (30) not null, 'user' CHAR (30), data CHAR (3000 ), parmiry by ('id '));
Next we will compile the session-saving file session_start.php.
The code is as follows: $ Con = mysql_connection ("127.0.0.1", "user", "pass ");
Mysql_select_db ("session ");
Function open ($ save_path, $ session_name) {return (true);} function close () {return (true);} function read ($ id) {if ($ result = mysql_query ("SELECT * FROM session WHERE id = '$ ID'") {if ($ row = mysql_felth_row ($ result ))
{Return $ row ["data"] ;}} else {return "" ;}} function write ($ id, $ sess_data) {if ($ result = mysql_query ("UPDATE session SET data = '$ sess_data' WHERE id = '$ ID'") {return true;} else {return false ;}} function destroy ($ id) {if ($ result = mysql_query ("DELETE * FROM session WHERE id = '$ ID'") {return true ;} else {return false ;}} /*************************************** ******
* WARNING-You will need to implement some *
* Sort of garbage collection routine here .*
**************************************** *****/
Function gc ($ maxlifetime) {return true;} session_set_save_handler ("open", "close", "read", "write", "destroy", "gc ");
Session_start ();
/& Amp; # Other Full Text & gt;

How to verify the session_id of the database?

Write a method in the public module of your backend. the function of this method is to compare the generated session_id with the session_id of the database. if it is the same, it indicates that you have logged on, but you have not logged on to the database. Then you can call this method in the module for login verification.

This article explains how ThinkPHP saves sessions to MYSQL through examples. the runtime environment used is ThinkPHP3.1.2 first...

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.