Home>Article>PHP Framework> ThinkPHP implements the method of storing SESSION in MYSQL

ThinkPHP implements the method of storing SESSION in MYSQL

高洛峰
高洛峰 Original
2016-12-22 10:36:41 2563browse

ThinkPHP implements the method of storing SESSION in MYSQL

First, the index.php is set to:


      

The config.php is set to:

'配置值' // 添加数据库配置信 '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.
This problem is solved. Do not set anything else, SessionDb.class.php will be loaded automatically.

In this way, the ThinkPHP call

session('session_name','session_value')

system will automatically store this session in the database created above.

Recommended related articles:The most complete collection of js interview questions in 2020 (latest)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:None Next article:thinkphp是什么