如何用couchbase存储session

有两种常见方式:
1.采用memcache模式连接couchbase 只需两句修改:

  1. ini_set('session.save_handler', 'memcache');
  2. ini_set('session.save_path', 'tcp://couchbase_host:9999');

注意里面的9999是couchbase 里面创建的bucket的对外memcache端口,这种访问方式运行以memcache兼容的模式访问couchbase。
2.采用couchbase扩展添加一个sessionhandler如下:

    1. /****
    2. * php storage session with couchbase
    3. * by fushanlang@gmail.com
    4. */
    5. /***
    6. * SessionHandlerInterface is a internal interface only for PHP_VERSION>=5.4.0
    7. * so if interface_exists() false we need to define by yourself.
    8. */
    9. if (!interface_exists('SessionHandlerInterface')) {
    10. interface SessionHandlerInterface
    11. {
    12. public function close();
    13. public function destroy($session_id);
    14. public function gc($maxlifetime);
    15. public function open($save_path, $session_id);
    16. public function  read($session_id);
    17. public function write($session_id, $session_data);
    18. }
    19. }
    20. /**
    21. * A reference implementation of a custom Couchbase session handler.
    22. */
    23. class CouchbaseSessionHandler implements SessionHandlerInterface
    24. {
    25. /**
    26. * Holds the Couchbase connection.
    27. */
    28. protected $_connection = null;
    29. /**
    30. * The Couchbase host and port.
    31. */
    32. protected $_host = null;
    33. /**
    34. * The Couchbase bucket name.
    35. */
    36. protected $_bucket = null;
    37. /**
    38. * The prefix to be used in Couchbase keynames.
    39. */
    40. protected $_keyPrefix = 'session:';
    41. /**
    42. * Define a expiration time of 10 minutes.
    43. */
    44. protected $_expire = 600;
    45. /**
    46. * Set the default configuration params on init.
    47. */
    48. public function __construct($host = '127.0.0.1:8091', $bucket = 'default')
    49. {
    50. $this->_host = $host;
    51. $this->_bucket = $bucket;
    52. }
    53. /**
    54. * Open the connection to Couchbase (called by PHP on `session_start()`)
    55. */
    56. public function open($savePath, $sessionName)
    57. {
    58. $this->_connection = new Couchbase($this->_host, '', '', $this->_bucket);
    59. return $this->_connection ? true : false;
    60. }
    61. /**
    62. * Close the connection. Called by PHP when the script ends.
    63. */
    64. public function close()
    65. {
    66. unset($this->_connection);
    67. return true;
    68. }
    69. /**
    70. * Read data from the session.
    71. */
    72. public function read($sessionId)
    73. {
    74. $key = $this->_keyPrefix . $sessionId;
    75. $result = $this->_connection->get($key);
    76. return $result ? : null;
    77. }
    78. /**
    79. * Write data to the session.
    80. */
    81. public function write($sessionId, $sessionData)
    82. {
    83. $key = $this->_keyPrefix . $sessionId;
    84. if (emptyempty($sessionData)) {
    85. return false;
    86. }
    87. $result = $this->_connection->set($key, $sessionData, $this->_expire);
    88. return $result ? true : false;
    89. }
    90. /**
    91. * Delete data from the session.
    92. */
    93. public function destroy($sessionId)
    94. {
    95. $key = $this->_keyPrefix . $sessionId;
    96. $result = $this->_connection->delete($key);
    97. return $result ? true : false;
    98. }
    99. /**
    100. * Run the garbage collection.
    101. */
    102. public function gc($maxLifetime)
    103. {
    104. return true;
    105. }
    106. }
    107. //usage example
    108. define('COUCHBASE_HOST_PORT','xxxxx:8091');
    109. define('COUCHBASE_BUCKET','session');
    110. if(class_exists('Couchbase')&&defined('COUCHBASE_HOST_PORT')&&defined('COUCHBASE_BUCKET')){
    111. $handler = new CouchbaseSessionHandler(COUCHBASE_HOST_PORT,COUCHBASE_BUCKET);
    112. if(version_compare(PHP_VERSION,'5.4.0')>=0){
    113. session_set_save_handler($handler,true);
    114. }else{
    115. session_set_save_handler(
    116. array($handler, 'open'),
    117. array($handler, 'close'),
    118. array($handler, 'read'),
    119. array($handler, 'write'),
    120. array($handler, 'destroy'),
    121. array($handler, 'gc'));
    122. }
    123. }
    124. session_start();
  1. 转自 http://www.fushanlang.com/blog/php-store-session-with-couchbase-2302/

PHP store session with couchbase的更多相关文章

  1. Node.js中的Session,不要觉得简单哦。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .学习网站上有对应 ...

  2. CI框架SESSION重写

    这个是我平时用的是 PHP 自己的 Session 机制,是真正的 PHP Session,存储在服务器端,而不是用的 cookie 接口完全兼容于 CI 2.0.x 的 Session 类库,也就是 ...

  3. PHP超级全局变量——Session 变量

    PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...

  4. APP store 官方统计工具的常见的Q&A

    Apple最近在iTunesConnect里最新发布了官方统计工具,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化,可以在App Ana ...

  5. Session fixation--wiki

    http://en.wikipedia.org/wiki/Session_fixation In computer network security, session fixation attacks ...

  6. 关于web会话中的session过期时间的设置

    关于web会话中的session过期时间的设置 1.操作系统: 步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉右键“属性”——〉主目录——〉配置— ...

  7. 转:cookie和session(二)——php应用

    文章来自于:http://blog.csdn.net/half1/article/details/21650211 本文将介绍cookie在session在php中的基本用法. 1.cookie   ...

  8. Asp.Net 禁用cookie后使用session

    原文地址:http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/using-cookie-less-session-in-Asp-Net/ H ...

  9. C#如何设置session过期时间

    1.操作系统  步骤:开始——〉管理工具——〉Internet信息服务(IIS)管理器——〉网站——〉默认网站——〉  右键“属性”——〉主目录——〉配置——〉选项——〉启用会话状态——〉会话超时(在 ...

随机推荐

  1. Xcode7 模拟器安装app (转)

    本文转自同事zz的博客http://archerzz.cn/ios/xcode-install-app.html 原由 很多时候,为了学习了解别人的app,很多时候我们是直接拿别人的app安装在自己的 ...

  2. ubuntu 编译源码坏境配置

    git checkout -b newlocal origin/q01v31source build/envsetup.shlunch msm8916_32-usermake -j4 make -j4 ...

  3. c++程序判断系统是Linux还是Windows

    用C++来实现,本来想了很多,后来越写越烂,而且结果总是不尽人意,干脆这样子好了: int main() { int judge = system("cls"); ) cout & ...

  4. 【java基础】java的构造函数

    java构造器用于创建类的实例,是创建对象的重要途径,因此,java类必须含有一个或一个以上的构造函数   当我们没有为类提供任何构造函数的时候,系统会自动为该类提供一个无参构造函数,当我们为类提供了 ...

  5. Android 6.0权限改变

    对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要访问通讯录.短 ...

  6. VNC SERVER配置

    vnc的配置网上有很多 普通用户的配置没有怎么写 根据下面这个说法 https://www.digitalocean.com/community/tutorials/how-to-install-an ...

  7. torrent

    The World's Largest BitTorrent System

  8. AS3和js相互通信要点分析

    目标:在html页面里可以使用事件来影响到swf文件的内容,swf文件也可以影响html里js代码的内容 一.新建flash文件,用Flash CC试用版新建一个TextArea.fla的源文件,不添 ...

  9. 【Logcat】在Android Studio中查看android APP 日志

    参考资料: http://jingyan.baidu.com/article/948f592408e07bd80ff5f9a9.html http://www.linuxidc.com/Linux/2 ...

  10. Response.Write("<script>alert('弹出对话框!')</script>") 后跟Response.Redirect("page.aspx");不能弹出对话框,直接跳转页面了 如何解?

    Response.Write和Response.Redirect一起用的时候就会这样,write脚本和redirect脚本不能同时使用,这样不会执行脚本,最好使用ClientScript 改进方法: ...