ThinkPHP implements the method of storing SESSION into MYSQL

  • 2021-07-09 07:39:06
  • OfStack

This paper explains the method of ThinkPHP to store SESSION into MYSQL with an example, and the running environment adopted is ThinkPHP version 3.1. 2

First set in index. php to:


<?php
define('APP_DEBUG', true);// Set to debug mode 
require '../ThinkPHP/ThinkPHP.php';// Set the entry file 
ini_set("session.save_handler", "user");// Settings PHP Adj. SESSION User-defined 

Set in config. php to:


<?php
return array(//' Configuration item '=>' Configuration value '
      //  Add a 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 ', //  User name 
  'DB_PWD'  => ' Your password ', //  Password 
  'DB_PORT'  => 3306, //  Port 
  'DB_PREFIX' => 'think_', //  Database table prefix 
'SESSION_OPTIONS'=>array(
    'type'=> 'db',//session Adopt database to save 
    'expire'=>1440,//session Expiration time, if not set php.ini Default values set in 
  ),
'SESSION_TABLE'=>'think_session', // It must be set so that if the data table cannot be found without prefix, this needs attention 
);
?>

The database setup uses DDL in SessionDb. class. php, but ENGINE=MyISAM DEFAULT CHARSET=utf8 is added after it


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 then find the think_session table in phpmyadmin, and we will be pleasantly surprised to find more data.
At this point, the problem is solved. Don't set anything else, SessionDb. class. php will load automatically.

So the call of ThinkPHP


session('session_name','session_value')

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


Related articles: