PHP store session with couchbase
如何用couchbase存储session
有两种常见方式:
1.采用memcache模式连接couchbase 只需两句修改:
- ini_set('session.save_handler', 'memcache');
- ini_set('session.save_path', 'tcp://couchbase_host:9999');
注意里面的9999是couchbase 里面创建的bucket的对外memcache端口,这种访问方式运行以memcache兼容的模式访问couchbase。
2.采用couchbase扩展添加一个sessionhandler如下:
- /****
- * php storage session with couchbase
- * by fushanlang@gmail.com
- */
- /***
- * SessionHandlerInterface is a internal interface only for PHP_VERSION>=5.4.0
- * so if interface_exists() false we need to define by yourself.
- */
- if (!interface_exists('SessionHandlerInterface')) {
- interface SessionHandlerInterface
- {
- public function close();
- public function destroy($session_id);
- public function gc($maxlifetime);
- public function open($save_path, $session_id);
- public function read($session_id);
- public function write($session_id, $session_data);
- }
- }
- /**
- * A reference implementation of a custom Couchbase session handler.
- */
- class CouchbaseSessionHandler implements SessionHandlerInterface
- {
- /**
- * Holds the Couchbase connection.
- */
- protected $_connection = null;
- /**
- * The Couchbase host and port.
- */
- protected $_host = null;
- /**
- * The Couchbase bucket name.
- */
- protected $_bucket = null;
- /**
- * The prefix to be used in Couchbase keynames.
- */
- protected $_keyPrefix = 'session:';
- /**
- * Define a expiration time of 10 minutes.
- */
- protected $_expire = 600;
- /**
- * Set the default configuration params on init.
- */
- public function __construct($host = '127.0.0.1:8091', $bucket = 'default')
- {
- $this->_host = $host;
- $this->_bucket = $bucket;
- }
- /**
- * Open the connection to Couchbase (called by PHP on `session_start()`)
- */
- public function open($savePath, $sessionName)
- {
- $this->_connection = new Couchbase($this->_host, '', '', $this->_bucket);
- return $this->_connection ? true : false;
- }
- /**
- * Close the connection. Called by PHP when the script ends.
- */
- public function close()
- {
- unset($this->_connection);
- return true;
- }
- /**
- * Read data from the session.
- */
- public function read($sessionId)
- {
- $key = $this->_keyPrefix . $sessionId;
- $result = $this->_connection->get($key);
- return $result ? : null;
- }
- /**
- * Write data to the session.
- */
- public function write($sessionId, $sessionData)
- {
- $key = $this->_keyPrefix . $sessionId;
- if (emptyempty($sessionData)) {
- return false;
- }
- $result = $this->_connection->set($key, $sessionData, $this->_expire);
- return $result ? true : false;
- }
- /**
- * Delete data from the session.
- */
- public function destroy($sessionId)
- {
- $key = $this->_keyPrefix . $sessionId;
- $result = $this->_connection->delete($key);
- return $result ? true : false;
- }
- /**
- * Run the garbage collection.
- */
- public function gc($maxLifetime)
- {
- return true;
- }
- }
- //usage example
- define('COUCHBASE_HOST_PORT','xxxxx:8091');
- define('COUCHBASE_BUCKET','session');
- if(class_exists('Couchbase')&&defined('COUCHBASE_HOST_PORT')&&defined('COUCHBASE_BUCKET')){
- $handler = new CouchbaseSessionHandler(COUCHBASE_HOST_PORT,COUCHBASE_BUCKET);
- if(version_compare(PHP_VERSION,'5.4.0')>=0){
- session_set_save_handler($handler,true);
- }else{
- session_set_save_handler(
- array($handler, 'open'),
- array($handler, 'close'),
- array($handler, 'read'),
- array($handler, 'write'),
- array($handler, 'destroy'),
- array($handler, 'gc'));
- }
- }
- session_start();
- 转自 http://www.fushanlang.com/blog/php-store-session-with-couchbase-2302/
PHP store session with couchbase的更多相关文章
- Node.js中的Session,不要觉得简单哦。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .学习网站上有对应 ...
- CI框架SESSION重写
这个是我平时用的是 PHP 自己的 Session 机制,是真正的 PHP Session,存储在服务器端,而不是用的 cookie 接口完全兼容于 CI 2.0.x 的 Session 类库,也就是 ...
- PHP超级全局变量——Session 变量
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...
- APP store 官方统计工具的常见的Q&A
Apple最近在iTunesConnect里最新发布了官方统计工具,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化,可以在App Ana ...
- Session fixation--wiki
http://en.wikipedia.org/wiki/Session_fixation In computer network security, session fixation attacks ...
- 关于web会话中的session过期时间的设置
关于web会话中的session过期时间的设置 1.操作系统: 步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉右键“属性”——〉主目录——〉配置— ...
- 转:cookie和session(二)——php应用
文章来自于:http://blog.csdn.net/half1/article/details/21650211 本文将介绍cookie在session在php中的基本用法. 1.cookie ...
- Asp.Net 禁用cookie后使用session
原文地址:http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/using-cookie-less-session-in-Asp-Net/ H ...
- C#如何设置session过期时间
1.操作系统 步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉 右键“属性”——〉主目录——〉配置——〉选项——〉启用会话状态——〉会话超时(在 ...
随机推荐
- an interview question(3)
最近看了些C面试题顺便复习一下C语言,现贴一些出来和大家分享. #include <stdio.h> void main () { ,,,,};--------- *(ptr++)+=; ...
- EJB 简介
EJB: 被称为java企业bean,服务器端组件,核心应用是部署分布式应用程序.用它部署的系统不限定平台.实际上ejb是一种产品,描述了应用组件要解决的标准 标准: 可扩展 (Scalable) ...
- TOEFL备考计划
一.总则: 1.坚持每一天,充分利用一切可以利用的时间学英语.没有持之以恒的学习和大量的时将做保障,一切都是空谈. 2.每天听写一篇文章,以此文章为中心,展开一天的学习. OG, TOP 3.听说读写 ...
- Linux时间设置及同步
Linux系统安装时选择的UTC时间是国际标准时间,而中国处于UTC+8时区,因此安装系统时不要选择UTC时区. 还有就是Linux有两个时钟: 1.Bios时钟及硬件时间 2.Kernel时钟及系统 ...
- angularJS 学习之路
AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序.也就是angularjs作用的入口 作用在什么标签或者整个body ...
- JQ_浏览器窗口改变触发
$(window).resize(function () { //当浏览器大小变化时 alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(docume ...
- js实现div层缓慢收缩与展开的方法
引脚本之家 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/T ...
- ubuntu14.04 Hadoop单机开发环境搭建MapReduce项目
Hadoop官网:http://hadoop.apache.org/ 目前最新的版本是Hadoop 3.0.0-alpha1前提:java 1.6 版本以上 首先从官网下载压缩包(hadoop-3.0 ...
- secureCRT远程登录工具的颜色配置(转载)
另外,字体和编码设置(如果需要显示中文):Options->Session Options->Appearance->font(字体:幼圆,字形:常规,大小:小三号,字符集:中文GB ...
- STM32 ADC 测电压
1. STM32F103 ADC 本例使用STM32F103芯片的PA1引脚测试模拟输入的电压值. 查看文档<STM32F103X.pdf>第31页,引脚定义图: 得知PA1使用ADC1的 ...