session入mysql
Posted wangjiuwang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session入mysql相关的知识,希望对你有一定的参考价值。
[php] view plain copy- <?php
- ini_set("session.save_handler","user");
- //session.gc_probability = 1 分子
- ini_set("session.gc_probability",1);
- //session.gc_divisor = 1000 分母
- ini_set("session.gc_divisor",2);
- //session.gc_maxlifetime = 1440 垃圾回收时间,session有效期
- session_set_save_handler( "open","close","read","write","destroy","gc" );
- //连接数据库
- function open()
- @$link = mysql_connect('127.0.0.1', 'root', 'root');
- mysql_query('set names utf8');
- mysql_query('use wangbin');
- function close()
- mysql_close();
- function read($sess_id)
- $sql = "select session_data from `session` where session_id = '$sess_id'";
- $result = mysql_query($sql);
- if($rows = mysql_fetch_assoc($result))
- return $rows['session_data'];
- else
- return '';
- function write($sess_id,$sess_data)
- $sql = "insert into `session` (session_id,session_data,session_time) values('$sess_id','$sess_data', now()) on duplicate key update session_data = '$sess_data' , session_time = now()"; //这是为了gc()
- return mysql_query($sql);
- function destroy($sess_id)
- echo __FUNCTION__;
- $sql = "delete from `session` where session_id = '$sess_id'";
- return mysql_query($sql);
- function gc($sess_id)
- $maxlifetime = ini_set("session.gc_maxlifetime");
- echo __FUNCTION__;
- $sql = "delete from `session` where now()-session_time > '$maxlifetime' ";
- return mysql_query($sql);
- header("content-type:text/html;charset=utf8");
- session_start();
- $_SESSION['name']='aa';
- //echo session_id();
- echo $_SESSION['name'];
-
以上是关于session入mysql的主要内容,如果未能解决你的问题,请参考以下文章