-
<?php /** * Created by PhpStorm. * User: user * Date: 2016/11/3 0003 * Time: 下午 8:12 */ 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 cms'); } 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){ // 讲session存入数据库 $read = mysql_query("select * from session where session_data='{$sess_data}'"); $info = mysql_fetch_assoc($read); // var_dump($info);die; if (empty($info)) { $sql1="delete from session where session_data != '{$sess_data}'"; mysql_query($sql1); $time = time(); mysql_query("insert into session (session_id, session_data, session_time) values ('{$sess_id}', '{$sess_data}', '{$time}')"); } else { $sql1="delete from session where session_data != '{$sess_data}'"; mysql_query($sql1); $sql = "update session set session_id='{$sess_id}',session_time=now() where session_data='{$sess_data}'"; 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); } if (!empty($_POST['name'])) { $name = $_POST['name']; session_start(); $_SESSION['name'] = "$name"; }