Home >Backend Development >PHP Tutorial >ThinkPHP implements the method of storing SESSION in MYSQL, thinkphpmysql_PHP tutorial
This article explains with examples how ThinkPHP implements storing SESSION in MYSQL. The operating environment used is ThinkPHP version 3.1.2
First set index.php to:
<?php define('APP_DEBUG', true);//设置为调试模式 require '../ThinkPHP/ThinkPHP.php';//设置入口文件 ini_set("session.save_handler", "user");//设置PHP的SESSION由用户定义
Set in config.php to:
<?php return array(//'配置项'=>'配置值' // 添加数据库配置信 'SHOW_PAGE_TRACE' =>true, 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'thinkphp', // 数据库名 'DB_USER' => '你的用户名', // 用户名 'DB_PWD' => '你的密码', // 密码 'DB_PORT' => 3306, // 端口 'DB_PREFIX' => 'think_', // 数据库表前缀缀 'SESSION_OPTIONS'=>array( 'type'=> 'db',//session采用数据库保存 'expire'=>1440,//session过期时间,如果不设就是php.ini中设置的默认值 ), 'SESSION_TABLE'=>'think_session', //必须设置成这样,如果不加前缀就找不到数据表,这个需要注意 ); ?>
The database setting uses the DDL in SessionDb.class.php, but ENGINE=MyISAM DEFAULT CHARSET=utf8
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 access your index.php and then find the think_session table in phpmyadmin. We will be surprised to find that there is more data.
The problem is now solved. Do not set anything else, SessionDb.class.php will be loaded automatically.
This is how ThinkPHP is called
session('session_name','session_value')
The system will automatically store this session in the database created above.
To establish the database and the table structure of the database, we can use any database that PHP can use. Because the combination of PHP and MySQL is the best, I will use MySQL for the example. Of course, you can change the name to another database according to your needs. At the same time, because MySQL does not have transaction functions, it is faster than other databases. However, saving session files does not require transaction processing, and I decided to do it better.
Create database:
Copy the code as follows: CREATE DATABASE 'session'; Create table structure CREATE TABLE 'session'( id CHAR(30) NOT NULL , 'user 'CHAR(30), data CHAR(3000) ,PARMIRY BY ('id') );
Let's write the file session_start.php to save the session
Copy the code 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();
/...The rest of the full text>>
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 they are the same, it means you have logged in, but if they are different, you have not logged in. Then call this method in the module that needs to verify login.