How to configure saving phpSession to redis

Source: Internet
Author: User
To save phpSession to redis, the first thing to do is to install redis.

Installation: http://redis.io/download

InstallationDownload, extract and compile Redis with:$ wget http://download.redis.io/releases/redis-2.8.19.tar.gz$ tar xzf redis-2.8.19.tar.gz$ cd redis-2.8.19$ makeThe binaries that are now compiled are available in the src directory. Run Redis with:$ src/redis-serverYou can interact with Redis using the built-in client:$ src/redis-cliredis> set foo barOKredis> get foo"bar"

Then install phpredis extension redis. so

Installation method reference: http://www.lecloud.net/post/3378834922/install-redis-php-extension-phpredis-with

/// // The following code is for reference to the website installation method, please adjust it according to your actual installation location, ///////////////// // cd/usr/localsudo mkdir srccd/usr/local /srcsudo mkdir phpredis-buildcd phpredis-buildsudo git clone -- depth 1 git: // github.com/owlient/phpredis.gitcd phpredissudo phpizesudo. /configuresudo makesudo make installcd/usr/local/lib/php5/sudo gedit php. if extension_dir is not configured for ini, configure cp redis in your extension path. so/usr/local/lib/php5/extension/join a row: extension = redis. so restart nginx and php-fpm and output phpinfo. the redis extension installation is complete.

Set php. ini

Find; session. save_handler = files and change it to session. save_handler = redis. changing the original session storage mode to the micro-redis mode will increase the speed, because redis is faster in memory than reading files.

Find; session. save_path = "/tmp" and change it to session. save_path = "tcp: // 127.0.0.1: 8888", which directs to the local tcp service, which will be started by our redis

Restart nginx and php-fpm and output phpinfo. the session storage mode and path are changed.

Start redis service

redis-server --port 8888 &

Compile php code

 'Fanqie', 'CCC '=> 'Hello redis'); $ redis = new redis (); $ redis-> connect ('2017. 127. 0.0.1 ', 8888); echo 'sessionid >>>>>>> PHPREDIS_SESSION :'. session_id (); echo'
'; Echo'
'; // Redis uses session_id as the key and stores echo in the form of string' obtained through redis in php >>>>>> '. $ redis-> get ('phpredis _ SESSION :'. session_id (); echo'
'; Echo'
'; Echo' obtained through php session >>>>>>
'; Echo'
';var_dump($_SESSION['test_session']);echo '
';

Page output, we can see that we have successfully stored and obtained the stored value through redis

Sessionid >>>>>>> PHPREDIS_SESSION: fmd75d3dfrlkp43eh567f7dsi5 obtained through redis in php >>>>>> test_session | a: 2: {s: 4: "name"; s: 6: "fanqie"; s: 3: "ccc"; s: 12: "hello redis";} obtained by using session in php >>>>>>> array (2) {["name"] => string (6) "fanqie" ["ccc"] => string (12) "hello redis "}

You can use the redis client to view the hello redis record that has been stored in the redis database.

dangcheng@dangcheng-pc:~$ redis-cli -p 8888127.0.0.1:8888> select 0OK127.0.0.1:8888> keys *1) "PHPREDIS_SESSION:fmd75d3dfrlkp43eh567f7dsi5"127.0.0.1:8888> get PHPREDIS_SESSION:fmd75d3dfrlkp43eh567f7dsi5"test_session|a:2:{s:4:\"name\";s:6:\"fanqie\";s:3:\"ccc\";s:12:\"hello redis \";}"127.0.0.1:8888>

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.