php怎么把session保存到MySql数据库中
发布:smiling 来源: PHP粉丝网 添加日期:2014-06-28 14:03:58 浏览: 评论:0
session我们多半是保存在服务器中,但是今天有一个功能就是需要把session保存在数据库中,这样可以实现同ie多浏览了,php中session默认的存储方式是硬盘,php也可以改变默认的存储方式,主要使用到session_set_save_handler方法,下面分享下如何将session保存到MySql数据库中的具体代码.
1.建session表,代码如下:
- CREATE TABLE `session` (
- `sessionid` varchar(128) NOT NULL,
- `uid` int(11) NOT NULL,
- `data` mediumblob NOT NULL,
- `timestamp` int(11) NOT NULL,
- `ip` varchar(15) NOT NULL,
- PRIMARY KEY (`sessionid`),
- KEY `time_session` (`timestamp`,`sessionid`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- //uid 是保留字段
2.自定义session类,代码如下:
- <?php
- class CustomSession{
- private static $db_host="localhost";
- private static $db_user="root";
- private static $db_password="";
- private static $database="session";
- private $conn;
- public static function getInstance(){
- static $instance=null;
- if($instance==null){
- $instance=new CustomSession();
- }
- return $instance;
- }
- public function __construct(){
- session_set_save_handler(
- array($this,"open"),
- array($this,"close"),
- array($this,"read"),
- array($this,"write"),
- array($this,"destroy"),
- array($this,"gc")
- );
- }
- public function __destruct(){
- session_write_close();
- }
- public function open(){
- $this->conn=mysql_connect(CustomSession::$db_host,CustomSession::$db_user,CustomSession::$db_password);
- mysql_select_db(CustomSession::$database,$this->conn);
- }
- public function close(){
- mysql_close($this->conn);
- }
- public function read($id){
- $escaped_id=mysql_escape_string($id);
- $res=$this->query("select * from `session` where `sessionid`='$escaped_id'");
- if($row=mysql_fetch_assoc($res)){
- $this->query("update `session` set `timetamp`=UTC_TIMESTAMP() where `sessionid`='$escaped_id'");
- return $row['data'];
- }
- return "";
- }
- public function write($id,$data){
- $query="replace into `session` (`sessionid`,`data`,`ip`,`timestamp`) values ('%s','%s','%s',UNIX_TIMESTAMP(UTC_TIMESTAMP()))";
- $this->query(sprintf($query,mysql_escape_string($id),mysql_escape_string($data),$_SERVER["REMOTE_ADDR"]));
- }
- public function destroy($id){
- $escaped_id=mysql_escape_string($id);
- $res=$this->query("delete from `session` where `id`='$escaped_id'");
- return (mysql_affected_rows($res)==1);
- }
- public function gc($lifetime){
- $this->query("delete from `session` where UNIX_TIMESTAMP(UTC_TIMESTAMP())-`timestamp` > $lifetime");
- }
- public function query($query){
- $res=mysql_query($query,$this->conn);
- return $res;
- }
- }
- ?>
3.测试程序,代码如下:
- <?php
- include('./CustomSession.class.php');
- CustomSession::getInstance();
- session_start();
- $_SESSION['username']='feng';
- print_r($_SESSION);
- ?>
运行测试程序后,查看数据库可以发现session表中已经增加了session记录.
Tags: session MySql数据库
相关文章
- ·session 的工作原理与session用法 (2013-11-12)
- ·PHP创建和使用session cookie变量(2013-11-13)
- ·session与cookie会话控制(2013-11-14)
- ·cookie和session 区别(2013-11-14)
- ·PHP 会话(session 时间设定)使用入门 (2013-11-14)
- ·关于使用session_start 出现的headers (2013-11-27)
- ·session Cannot send session cache limiter 错误提示(2013-11-28)
- ·session_start() [function.session-start]: Cannot send session cookie(2013-11-29)
- ·Warning: session_destroy() [function.session-destroy]: Trying to destroy(2013-11-29)
- ·php中session引起错误问题集锦(2013-11-30)
- ·php session永不过期的设置方法(2013-11-30)
- ·PHP中session共享和登录验证的实现方法(2013-12-02)
- ·php中session超时严格控制实例(2013-12-02)
- ·PHP中header和session_start前不能有输出的原因(2013-12-03)
- ·Deprecated: Function session_register() is deprecated(2013-12-04)
- ·Warning: session_start() [function.session-start](2013-12-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)