---
title: "PHP memcached的Session的使用与配置"
author: "bardo"
date: "2011-02-25T13:56:47Z"
tags: []
categories: ["编程语言"]
slug: "php-memcached-session"
draft: false
lastmod: "2011-02-25T13:56:47Z"
id: 929762
url: "https://bardo.iteye.com/blog/929762"
---

在[http://bardo.iteye.com/blog/914110](http://bardo.iteye.com/blog/914110)这篇文章中，我们已讲了 PHP memcached 的安装方法。 PHP memcached的Session的使用，有哪些问题，这里简述一下：

memcached的session，第一大优点是比PHP session要高效，快速。其次是可以方便实现多主机session共享  
使用以下方式使用memcached的session:  
单一站点的服务器，可以在php.ini中使用：  
session.save\_handler "memcached"  
session.save\_path  "host1:11211,host2:11211"  
 session.save\_path 中的参数必须是逗号分隔的主机名加端口。主机名也可以使用IP地址。  
虽然，服务端都是memcached,但不同于memcache扩展，它不需要在save\_path中指定通讯协议  
如果服务器有多主机，你要给你当前的虚拟主机配置，则可以配置到.htaccess文件中。格式如下：  
php\_value session.save\_handler "memcached"  
php\_value session.save\_path  "host1:11211,host2:11211"  
 注意事项：此时，php.ini中的 session.use\_cookies的值必须是1。如果系统中此值不是1，那么在

.htaccess文件中要增加：  
php\_value session.use\_cookies = 1  
 如果.heaccess报错，可以参考下面的例子进行修改：  
\# PHP 4, Apache 1.  
&lt;IfModule mod\_php4.c&gt;  
  php\_value session.save\_handler "memcached"  
&lt;/IfModule&gt;  
\# PHP 4, Apache 2.  
&lt;IfModule sapi\_apache2.c&gt;  
  php\_value session.save\_handler "memcached"  
&lt;/IfModule&gt;  
\# PHP 5, Apache 1 and 2.  
&lt;IfModule mod\_php5.c&gt;  
  php\_value session.save\_handler "memcached"  
&lt;/IfModule&gt;  
 .htaccess可以控制到目录级别。而同时，还有可以直接在php代码中控制的方式：  
ini\_set("session.save\_handler", "memcached");  
ini\_set("session.save\_path", "host1:11211,host2:11211");  
然而，这也要看你PHP的运行模式，如果是 mod\_php方式, .htaccess是可以修改的, 但是如果你安装的是  
fast\_cgi，那么，你需要php\_fpm来管理不同的php.ini。如果是mod\_fcgid,那么，就要用FcgidInitialEnv

PHPRC "/dirOfPhiIni" 来设置不同的php.ini，但是，FcgidInitialEnv是新版本的中的命令。老板本的则是

DefaultInitEnv。有些版本有BUG，不一定能设置成功。而对于suPHP，则也需要在其配置中指定不同的配置

目录。

以下是 mod\_fcgid 的一个配置实例：

#httpd.conf

LoadModule php5\_module /php/php5apache2\_2.dll  
LoadModule fcgid\_module modules/mod\_fcgid.so  
PHPIniDir /php

&lt;IfModule fcgid\_module&gt;  
\# whatever directives wanted bla bla bla  
\# Use same php.ini as mod\_php globally for mod\_fcgid  
FcgidInitialEnv PHPRC "/php"  
&lt;/IfModule&gt;

#httpd-vhost.conf

#using mod\_php and /php/php.ini  
&lt;VirtualHost \*:80&gt;  
    ServerName php.example.com  
    DocumentRoot "/home/htdocs"  
&lt;/VirtualHost&gt;

#using mod\_fcgid and /home/user1/php.ini  
&lt;VirtualHost \*:80&gt;  
    ServerName user1.example.com  
    DocumentRoot "/home/user1/htdocs"  
      FcgidInitialEnv PHPRC "/home/user1"  
      AddHandler fcgid-script .php  
      FcgidWrapper "/php/php-cgi.exe" .php  
&lt;/VirtualHost&gt;

#using mod\_fcgid and /home/user2/php.ini  
&lt;VirtualHost \*:80&gt;  
    ServerName user2.example.com  
    DocumentRoot "/home/user2/htdocs"  
      FcgidInitialEnv PHPRC "/home/user2"  
      AddHandler fcgid-script .php  
      FcgidWrapper "/php/php-cgi.exe" .php  
&lt;/VirtualHost&gt;

#using mod\_fcgid and global php.ini  
&lt;VirtualHost \*:80&gt;  
    ServerName user3.example.com  
    DocumentRoot "/home/user3/htdocs"  
      AddHandler fcgid-script .php  
      FcgidWrapper "/php/php-cgi.exe" .php  
&lt;/VirtualHost&gt;    
如果这些所有针对站点设置的都行不通，那你只能够使用代码的方法区别对待了。  
代码中使用，一种可以直接调用ini\_set, 还有一种，就是用 memcached自己写一个 session类，然后，映身

session函数到你写的session类中，这一点网上也是有例子的，所以，也不用多讲了。