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

ThinkPHP implements the method of storing SESSION in MYSQL

高洛峰
Release: 2020-07-31 15:10:10
Original
2705 people have browsed it

ThinkPHP implements the method of storing SESSION in MYSQL

First, the index.php is set to:

<?php
define(&#39;APP_DEBUG&#39;, true);//设置为调试模式
require &#39;../ThinkPHP/ThinkPHP.php&#39;;//设置入口文件
ini_set("session.save_handler", "user");//设置PHP的SESSION由用户定义
Copy after login

The config.php is set to:

<?php
return array(//&#39;配置项&#39;=>&#39;配置值&#39;
      // 添加数据库配置信
  &#39;SHOW_PAGE_TRACE&#39; =>true,
  &#39;DB_TYPE&#39;  => &#39;mysql&#39;, // 数据库类型
  &#39;DB_HOST&#39;  => &#39;localhost&#39;, // 服务器地址
  &#39;DB_NAME&#39;  => &#39;thinkphp&#39;, // 数据库名
  &#39;DB_USER&#39;  => &#39;你的用户名&#39;, // 用户名
  &#39;DB_PWD&#39;  => &#39;你的密码&#39;, // 密码
  &#39;DB_PORT&#39;  => 3306, // 端口
  &#39;DB_PREFIX&#39; => &#39;think_&#39;, // 数据库表前缀缀
&#39;SESSION_OPTIONS&#39;=>array(
    &#39;type&#39;=> &#39;db&#39;,//session采用数据库保存
    &#39;expire&#39;=>1440,//session过期时间,如果不设就是php.ini中设置的默认值
  ),
&#39;SESSION_TABLE&#39;=>&#39;think_session&#39;, //必须设置成这样,如果不加前缀就找不到数据表,这个需要注意
);
?>
Copy after login

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;
Copy after login

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(&#39;session_name&#39;,&#39;session_value&#39;)
Copy after login

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)

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template