redis combined with php

redis combined with php

How does redis combine with php and realize session sharing?

1. Redis extension module installation

1. First install the redis extension of php, which is the same as the memcached installation.

1. you can go to the official website of pecl.php.net to download the installation package, the version 2.8.21 I downloaded here # tar zxf redis-2.8.21.tgz # cd redis-2.8.21 #/usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make && make installl installation is complete

Then modify php.ini, I have modified extension_dir = "./" to

extension_dir = "/usr/local/php/extensions".

Now add a line extension="redis.so" to save

/usr/local/php/bin/php -m |grep redis can check whether the extension module is added successfully.

The php script test can follow the same test as the previous memcached article. I won’t talk about it here. You can check the previous article-"memcached combined with php"

2.Redis realizes session sharing

Join in the apache virtual host (lamp environment test)

php_valuesession.save_handler "redis"

php_valuesession.save_path "tcp://127.0.0.1:6379"

Or join the pool corresponding to php-fpm.conf (lnmp environment test)

php_value[session.save_handler] = redis

php_value[session.save_path] = "tcp://127.0.0.1:6379"

3. Redis storage session test

First edit a simple php test file

vim session.php

#############

<?php

session_start();

if (!isset($_SESSION['TEST'])) {

$_SESSION['TEST'] = time();

}

$_SESSION['TEST3'] = time();

print $_SESSION['TEST'];

print "<br><br>";

print $_SESSION['TEST3'];

print "<br><br>";

print session_id();

?>

###################

curl localhost/session.php

The output is similar to 1469333260<br><br>1469333260<br><br>jaesi8ummenfqbcnojvc6935p7

Connect to redis:/usr/local/redis/bin/redis-cli

127.0.0.1:6379> keys *//List all keys

"PHPREDIS_SESSION:jaesi8ummenfqbcnojvc6935p7"

127.0.0.1:6379> get PHPREDIS_SESSION:jaesi8ummenfqbcnojvc6935p7

"TEST|i:1469333260;TEST3|i:1469333260;"

You can also use the browser to test the input: IP/session.php

Then check on redis to see if there is an extra key that is the same as that displayed on the web page. Get this key can get the value result and the browser display is also consistent.

Reference: https://cloud.tencent.com/developer/article/1480134 redis combined with PHP-Cloud + Community-Tencent Cloud