PHP Redis Storage Session Example detailed

Source: Internet
Author: User
Tags php redis phpinfo redis

Install the Redis service and the PHP redis extension

One: Redis installation

Download, extract and compile Redis with:
$ wget http://download.redis.io/releases/redis-3.0.4.tar.gz
$ tar xzf redis-3.0.4.tar.gz
$ CD redis-3.0.4
$ make
The binaries that are now compiled are available in the SRC directory. Run Redis with:
$ src/redis-server
Can interact with Redis using the built-in client:
$ src/redis-cli
Redis> set Foo Bar
Ok
Redis> get foo
"Bar"

More:http://www.redis.io/download

Second, PHP expansion:

More version: Http://pecl.php.net/package/redis
wget http://pecl.php.net/get/redis-2.2.5.tgz
#解压
Tar zxvf redis-2.2.5.tgz
#进入安装目录
CD redis-2.2.5
/usr/local/php/bin/phpize
#配置
./configure--with-php-config=/usr/local/php/bin/php-config
#编译安装
Make && make install

After the installation is complete, the following installation path appears

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

Configuring PHP Support


#编辑配置文件, add the following on the last line

Vim/usr/local/php/etc/php.ini
extension= "Redis.so"

This time phpinfo () can see the redis extension.

Redis Small Example:

$redis = new Redis ();
$test = $redis->connect (' 127.0.0.1 ', 6379);
Var_dump ($test);

$result = $redis->set (' Test ', ' 111cn.net ');
Var_dump ($result); Result: BOOL (TRUE)

$result = $redis->get (' Test ');
Var_dump ($result); Result: String (one) "111cn.net"

Modify the php.ini settings

Session.save_handler = Redis
Session.save_path = "tcp://127.0.0.1:6379"

After modification, restart Php-fpm,phpinfo () to see that the session is stored in Redis.

If you do not want to modify php.ini, you can add it in your code:

Ini_set ("Session.save_handler", "Redis");
Ini_set ("Session.save_path", "tcp://127.0.0.1:6379");

If redis.conf sets the connection password (requirepass), the Save_path of the session needs to be modified to: the Tcp://127.0.0.1:6379?auth=requirepass value.
If you choose Redis Database, Session.save_path = "tcp://xx.xx.xx.xx:6789?database=11", and so on.

To view the value of the Redis store session:

<?php
Session_Start ();
$_session[' sessionid '] = ' www.111cn.net ';

$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
Redis with phpredis_session: Add session_id as key, and is stored as string
echo $redis->get (' phpredis_session: '. session_id ()); Output www.111cn.net
?>

See someone on the Internet question Redis storage session value There is concurrency consistency problem (file storage sessions have file locks to handle), this piece is not studied.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.