≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php将session保存到数据库的简单示例

人气:847 时间:2018-09-22

这篇文章主要为大家详细介绍了php将session保存到数据库的简单示例,具有一定的参考价值,可以用来参考一下。

对php中将session保存到数据库的代码感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
我们可以使用session_set_save_handler()来注册连接数据的函数。下面是完整的演示代码

/**
 * php中将session保存到数据库的代码
 *
 * @param 
 * @arrange 512-笔记网: q1010.com
 **/
// 'sessions' table schema
// create table sessions (
//   session_id char(32) not null,
//   session_data text not null,
//   session_expiration int(11) unsigned not null,
//   primary key (session_id));
//
include_once 'DB.php';
// Global Variables
$dbh = NULL;
function on_session_start ($save_path, $session_name) {
	global $dbh;
	$dbh = DB::connect('mysql://user:secret@localhost/SITE_SESSIONS',
					   true);
	if (DB::isError($dbh)) {
		die(sprintf('Error [%d]: %s',
					$dbh->getCode(), $dbh->getMessage()));
	}
}
function on_session_end ()
{
   // Nothing needs to be done in this function
   // since we used persistent connection.
}
function on_session_read ($key)
{
	global $dbh;
	$stmt = "select session_data from sessions";
	$stmt .= " where session_id = '$key'";
	$stmt .= " and session_expiration > now()";
	$sth = $dbh->query($sth);
	$row = $sth->fetchRow(DB_FETCHMODE_ASSOC);
	return $row['session_data'];
}
function on_session_write ($key, $val)
{
	global $dbh;
	$val = addslashes($val);
	$insert_stmt = "insert into sessions values('$key', '$val', now() + 3600)";
	$update_stmt = "update sessions set session_data = '$val', ";
	$update_stmt .= "session_expiration = now() + 3600 ";
	$update_stmt .= "where session_id = '$key'";
	// First we try to insert, if that doesn't succeed, it means
	// session is already in the table and we try to update
	if (DB::isError($dbh->query($insert_stmt)))
		$dbh->query($update_stmt);
}
function on_session_destroy ($key)
{
	global $dbh;
   $stmt = "delete from sessions where session_id = '$key'";
   $dbh->query($stmt);
}
function on_session_gc ($max_lifetime)
{
	global $dbh;
	// In this example, we don't use $max_lifetime parameter
	// We simply delete all sessions that have expired
	$stmt = "delete from sessions where session_expiration < now()";
	$dbh->query($stmt);
}
session_start ();
// Register the $counter variable as part
// of the session
session_register ("counter");
// Set the save handlers
session_set_save_handler ("on_session_start",   "on_session_end",
						  "on_session_read",    "on_session_write",
						  "on_session_destroy", "on_session_gc");
// Let's see what it does
$counter++;
print $counter;
session_destroy();

/***   来自四海网(www.q1010.com)   ***/

 

本文来自:http://www.q1010.com/173/1043-0.html

注:关于php将session保存到数据库的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:session

您可能感兴趣的文章

  • php保存信息到当前Session的简单示例
  • php读写session入门实例
  • php清除和销毁session的简单示例
  • php查看当前Session的ID入门实例
  • php 完整登录函数的简单示例(带session写入,mysql数据库查询)
  • PHP Session操作类用法示例
  • php 通过Session检查用户是否已经登录的简单示例
  • php 使用session实现数据库交互类的简单示例
  • PHP 解决错误:Warning: session_start()... 的方法
  • PHP 将session有效范围设置成整个站点目录的简单示例
上一篇:php限制上传文件类型并保存上传文件
下一篇:php获得文件大小和文件创建时间的简单示例
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。