Thinkphp to implement the method of storing session in MySQL _php instance

Source: Internet
Author: User

In this paper, an example of thinkphp implementation will be stored in the MySQL method, the operating environment is ThinkPHP3.1.2 version

First set in index.php to:

<?php
define (' App_debug ', true);//set to debug mode
require ' ... /thinkphp/thinkphp.php '//Set Portal file
ini_set ("Session.save_handler", "user");/set PHP session by user-defined

Set in config.php to:

<?php return Array (///
' config item ' => ' config value '
      ////Add Database Configuration Letter
  ' Show_page_trace ' =>true,
  ' db_type '  = > ' mysql ',//database type
  ' db_host '  => ' localhost ',//server address
  ' db_name '  => ' thinkphp ',//database name c15/> ' db_user '  => ' your username ',//username
  ' db_pwd ' => '  your password ',//password
  ' db_port ' =>  3306,//Port c21/> ' Db_prefix ' => ' think_ ',//database table prefix suffix
' session_options ' =>array (
    ' type ' => ' DB ',// Session using the database to save
    ' expire ' =>1440,//session expiration time, if not set is the default value of PHP.ini
  ,
' session_table ' => ' Think_session ',//must be set to this, if no prefix can not find the data table, this needs attention
);
? >

The database setup takes the DDL in SessionDb.class.php, but the Engine=myisam DEFAULT Charset=utf8 is appended

CREATE TABLE think_session (
    session_id varchar (255) NOT NULL,
    Session_expire int (one) 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, we will be pleasantly surprised to find a lot of data.
This problem is settled. Other do not set, SessionDb.class.php will automatically load.

So the thinkphp call

Session (' Session_name ', ' Session_value ')

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

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.