Home  >  Article  >  Backend Development  >  PHP memcached的Session的施用与配置

PHP memcached的Session的施用与配置

WBOY
WBOYOriginal
2016-06-13 13:04:39649browse

PHP memcached的Session的使用与配置

在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.

? php_value session.save_handler "memcached"

# PHP 4, Apache 2.

? php_value session.save_handler "memcached"

# PHP 5, Apache 1 and 2.

? php_value session.save_handler "memcached"

?.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


# whatever directives wanted bla bla bla
# Use same php.ini as mod_php globally for mod_fcgid
FcgidInitialEnv PHPRC "/php"


#httpd-vhost.conf

#using mod_php and /php/php.ini

??? ServerName php.example.com
??? DocumentRoot "/home/htdocs"

#using mod_fcgid and /home/user1/php.ini

??? ServerName user1.example.com
??? DocumentRoot "/home/user1/htdocs"
????? FcgidInitialEnv PHPRC "/home/user1"
????? AddHandler fcgid-script .php
????? FcgidWrapper "/php/php-cgi.exe" .php

#using mod_fcgid and /home/user2/php.ini

??? ServerName user2.example.com
??? DocumentRoot "/home/user2/htdocs"
????? FcgidInitialEnv PHPRC "/home/user2"
????? AddHandler fcgid-script .php
????? FcgidWrapper "/php/php-cgi.exe" .php

#using mod_fcgid and global php.ini

??? ServerName user3.example.com
??? DocumentRoot "/home/user3/htdocs"
????? AddHandler fcgid-script .php
????? FcgidWrapper "/php/php-cgi.exe" .php
?
如果这些所有针对站点设置的都行不通,那你只能够使用代码的方法区别对待了。
代码中使用,一种可以直接调用ini_set, 还有一种,就是用 memcached自己写一个 session类,然后,映身

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn