1. 程式人生 > >php使用memcache存儲session

php使用memcache存儲session

測試文件 -s libev round table 編輯 bin 3.0 title

安裝完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/

技術分享圖片

定位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

技術分享圖片

編輯測試文件

[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]#

通過瀏覽器進行測試訪問

技術分享圖片

php使用memcache存儲session