php使用memcache存储session

安装完php的时候需要安装memcache的扩展

wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz

tar zxf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local
make && make install

wget http://pecl.php.net/get/memcache-3.0.8.tgz

tar zxf memcache-3.0.8.tgz
cd memcache-3.0.8
locate phpize
/usr/local/php/bin/phpize
locate php-config
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

安装完扩展之后会出现一个路径

/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

image

定位php.ini的位置

[root@haha-42-12 memcache-3.0.8]# locate php.ini
/etc/puppet/modules/php/files/etc/php.ini
/usr/local/php/etc/php.ini
[root@haha-42-12 memcache-3.0.8]# cat /usr/local/php/etc/php.ini|grep extension

修改如下配置

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"
extension = memcache.so

image

编辑测试文件

[root@haha-42-12 default]# cat test.php
<?php
$ip = '192.168.42.8';
$port = 11211;
ini_set("session.save_handler", "memcache");
ini_set("session.save_path", "tcp://$ip:$port");
session_start();
$_SESSION['time'] = time();
 
print 'time:' . $_SESSION['time'];
print "<br>";
$key = session_id();
print 'session_id:' . $key;
print "<br>";
$memcache = new Memcache;
$memcache->addServer($ip, $port);
echo "ke: $key value:" . $memcache -> get($key);
?>
[root@haha-42-12 default]#

通过浏览器进行测试访问

image

猜你喜欢

转载自blog.51cto.com/jiandan/2155874