本文以实例讲解了thinkphp实现将session存入mysql的方法,所采用的运行环境是thinkphp3.1.2版
首先index.php中设置为:
<?php
define('app_debug', true);//设置为调试模式
require '../thinkphp/thinkphp.php';//设置入口文件
ini_set("session.save_handler", "user");//设置php的session由用户定义
在config.php中设置为:
<?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', //必须设置成这样,如果不加前缀就找不到数据表,这个需要注意
);
?>
数据库设置采用sessiondb.class.php中的ddl,不过后面加了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;
现在访问你的 index.php 后再在 phpmyadmin 中找到 think_session 表,我们会惊喜的发现多了条数据。
至此问题搞定。其他不要设置了,sessiondb.class.php会自动加载.
这样thinkphp的调用
session('session_name','session_value')
系统就会自动把这个session存储上面创建的数据库中。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!