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/
转载于:https://www.cnblogs.com/fushanlang/p/3340521.html
PHP store session with couchbase相关推荐
- Django源码分析5:session会话中间件分析
django源码分析 本文环境python3.5.2,django1.10.x系列 1.这次分析django框架中的会话中间件. 2.会话保持是目前框架都支持的一个功能,因为http是无状态协议,无法 ...
- php session 在线用户,php – 使用$_SESSION超全局获取当前在线用户并将其重新设置回当前会话数据是否很难?...
我正在使用数据库来存储会话数据.我创建了一些函数来通过当前活动会话进行查询,并返回一些要在当前页面上使用的值.使用单个查询就可以轻松获得打开会话的数量 "从UserSessions中选择Se ...
- php记住表单数据cookie,【PHP基础】cookies和session
1.Cookies cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 co ...
- Laravel源码分析之Session
由于HTTP最初是一个匿名.无状态的请求/响应协议,服务器处理来自客户端的请求然后向客户端回送一条响应.现代Web应用程序为了给用户提供个性化的服务往往需要在请求中识别出用户或者在用户的多条请求之间共 ...
- Laravel核心解读--Session源码解析
Session 模块源码解析 由于HTTP最初是一个匿名.无状态的请求/响应协议,服务器处理来自客户端的请求然后向客户端回送一条响应.现代Web应用程序为了给用户提供个性化的服务往往需要在请求中识别出 ...
- cookie、session和token
https://zhuanlan.zhihu.com/p/25495290?utm_source=wechat_session&utm_medium=social 一.cookie 众所周知, ...
- PHP 解决session 死锁
今天在开发碰到个棘手的问题 . 当异步请求后台处理一个大数据量操作时 请求其他控制器都没返回信息了..起初以为是Ext 框架设置了ajax同步造成的. 后来发现时session 死锁造成其他控制器在 ...
- 在yog2框架中自建模块,以实现socket.io与express共享session中间件
导引 最近遇到了一个需要在yog2框架中增加websocket服务的案子,此文主要讲在案子过程中碰到的一些问题和解决方案 前言 一.如果你只是想要解决socket.io与express共享解析sess ...
- PHP——session
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...
最新文章
- 20155222 第二周测试
- C ++ 11基于反向范围的for循环
- 为什么微信内无法分享转发网址,详谈微信网址防屏蔽的办法
- SAP CRM 中间件里的发布-订阅者模式
- 手机键鼠映射软件_吃鸡,我最专业!---盖世小鸡键鼠吃鸡套装评测
- 线程的组成 java 1615387415
- TopicDeletionManager分析
- QT缓存QGraphicsView的背景图问题
- 四级信息安全工程师基础知识
- Graphpad Prism 图文教程:XY图的属性设置
- Genotype陨石的秘密
- 亦大亦小如你--MySQL
- history对象back()、forward()、go()方法
- 不得转载可以转发吗_微信公众号可以复制转载别人的文章吗?如何转发其他公众号文章?...
- root-me web-client过关斩将
- 改造宿舍门成为智能门(未完)
- 串流直播流媒体视频发布平台功能模块和产品技术参数
- node-red安装
- docker images 导入导出
- p82 红蓝对抗-蓝队atckDs蜜罐威胁情报