使用MySQL开始PHP会话
http://tech.ddvip.com 2007年08月27日 社区交流
内容摘要:对于 PHP 开发来说,保存会话用 MySQL 是一个非常不错的选择。MySQL 提供一种建立在内存中的表类型 Heap,如果每条会话数据量很小的话,可以考虑用这种类型的表来进一步优化性能。
用户会话数据在会话处理函数中都是序列化之后的,要取出其中的某个会话变量,可以对其进行反序列化,默认是 php 序列化方式,可以用 session::unserialize 函数来反序列化。
下面的代码定义了一个用 MySQL 来处理 PHP 会话的类,其中所使用的 class_mysql.php 请参见 《超级简单但超级实用的 PHP 的 mysql 类》 。
<?php
/**
* @author 马秉尧
* @copyright (C) 2005 CoolCode.CN
*/
require_once("class_mysql.php");
class session {
var $db;
function session(&$db) {
$this->db = &$db;
session_module_name('user');
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
session_start();
}
function unserialize($data_value) {
$vars = preg_split(
'/([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)|/',
$data_value, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE
);
for ($i = 0; $vars[$i]; $i++) {
$result[$vars[$i++]] = unserialize($vars[$i]);
}
return $result;
}
function open($path, $name) {
return true;
}
function close() {
return true;
}
function read($session_id) {
$session_id = $this->db->escape_string($session_id);
if ($row = $this->db->query("select * from `sessions` where `session_id` = '$session_id' limit 1")) {
return $row['data_value'];
}
else {
$this->db->query("insert into `sessions` set `session_id` = '$session_id'");
return "";
}
}
function write($session_id, $data_value) {
$data = $this->unserialize($data_value);
$session_id = $this->db->escape_string($session_id);
$data_value = $this->db->escape_string($data_value);
$this->db->query("update `sessions` set "
. "`user_id` = '{$data['user_id']}', "
. "`data_value` = '$data_value', "
. "`last_visit` = null "
. "where `session_id` = '$session_id'");
return true;
}
function destroy($session_id) {
$session_id = $this->db->escape_string($session_id);
$this->db->query("delete from `sessions` where `session_id` = '$session_id'");
return true;
}
function gc($lifetime) {
$this->db->query("delete from `sessions` where unix_timestamp(now()) - unix_timestamp(`last_visit`) > $lifetime");
return true;
}
// get sessions by user_id
function get($user_id) {
$user_id = $this->db->escape_string($user_id);
return $this->db->query("select * from `sessions` where `user_id` = '$user_id'");
}
// get sessions list
function lists($page, $rows) {
if ($page == 0) {
return $this->db->query("select * from `sessions` order by `user_id`");
}
else {
$start = ($page - 1) * $rows;
return $this->db->query("select * from `sessions` order by `user_id` limit $start, $rows");
}
}
}
?>这个类的使用很简单,在原来使用 session_start 的地方,替换成 $session = new session($db) 就可以了。$db 表示 sessions 表所在的数据库。
另外可以用 get 方法来获取某个用户的所有会话信息,通过 lists 方法来得到所有用户会话列表。这样就可以方便的管理用户会话了。
来源:coolcode 作者:andot 责编:豆豆技术应用
正在加载评论...
- php 正则表达式
- php 入门教程
- php 安装配置
- php 函数专题
- php 函数大全(EN)
- php 5.0 中文手册
- php 4.0 中文手册
- php 程序编码规范标准
- php 常见错误
- php 中文乱码
- php Apache 安装配置
- linux php 安装配置
- windows php 安装配置
- php 十天入门教程
- php 学习笔记
- php smarty 教程
- php 分页专题
- php 类
- php 变量
- php 常量
- php 数组
- php 脚本
- php 入门实例
- php 字符串
- php.ini 配置
- php xml 专题
- php session 教程
- php 对象模型
- 更多php专题……