Summary of setting sessions to use memcache for storage in php. sessionmemcache_PHP tutorial

Source: Internet
Author: User
In php, set the session to use memcache for storage. sessionmemcache. Summary of how to set the session to use memcache for storage in php, sessionmemcachememcached provides a custom session processor that can be used to store user session data to the memcached server. in php, set the session and use memcache for storage. sessionmemcache

Memcached provides a custom session processor that can be used to store user session data to the memcached server. A completely independent memcached instance will be used internally, so you can set a different server pool if needed. The session key is stored under the prefix memc. sess. key.. Therefore, if you use the same server pool for the session and the general cache, pay attention to this. Another reason for session separation is that when the cache is full on the memcached server, your session may be kicked out of the cache, this may cause users to be inexplicably disconnected.

Session. save_handler string

Set memcached to enable the memcached session processor.

Session. save_path string

Defines a comma-separated session cache server pool in the hostname: port format, for example, "sess1: 11211, sess2: 11211 ".

Method I: global settings in php. ini

session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211" 

Method II:. htaccess under a directory

php_value session.save_handler "memcache" php_value session.save_path "tcp://127.0.0.1:11211" 

Method III: or in an application

ini_set("session.save_handler", "memcache"); ini_set("session.save_path", "tcp://...:"); 

Multiple memcached servers are separated by commas (,), which are the same as those described in the Memcache: addServer () document, you can include additional parameters such as "persistent", "weight", "timeout", and "retry_interval". for example, "tcp: // host1: port1? Persistent = 1 & weight = 2, tcp: // host2: port2 ".

If the installed PECL is memcached (the extension dependent on the libmemcached Library), the configuration should be

Ini_set ("session. save_handler "," memcached "); // It is memcached, not memcache ini_set (" session. save_path "," 127.0.0.1: 11211 "); // do not use tcp: [/B]

Code example (not dependent on the libmemcached Library)

<?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = time(); } $_SESSION['TEST'] = time(); print $_SESSION['TEST']; print "

"; print $_SESSION['TEST']; print "

"; print session_id(); ?>

Use sessionid to query in memcached:

<?php $memcache = memcache_connect('localhost', ); var_dump($memcache->get('ccedecbceebe')); $memcache->set('aaaa', 'hello everyone'); var_dump($memcache->get('aaaa')); ?>

You will see

String (37) "TEST | I: 1177556731; TEST3 | I: 1177556881 ;"

This output proves that the session works normally.

The following two Usage examples show you how to use memcached in php to store sessions.

I,

ini_set("session.save_handler", "memcache");ini_set("session.save_path","tcp://127.0.0.1:11211");

Multiple memcached instances

ini_set("session.save_path","tcp://127.0.0.1:11211,tcp://127.0.0.1:11211");

II,

ini_set("session.save_handler", "memcached");ini_set("session.save_path","...:");

Multiple memcached instances

ini_set("session.save_path","127.0.0.1:11211,127.0.0.1:11211");
Articles you may be interested in:
  • Use Memcached to implement session mechanism in php to replace native session support in PHP
  • Explanation of using memcache to store sessions based on php
  • Php puts session in memcached setting method
  • Three configuration methods for storing sessions using memcache in PHP

Memcached provides a custom session processor that can be used to store user session data to the memcached server...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.