Let PHP Session into Redis configuration method

Source: Internet
Author: User
Tags php session install redis
The first thing to do is to install Redis

Installation method: 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 is now compiled is 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 BA Rokredis> get foo "bar"

Then install the Phpredis extension redis.so

Installation Method Reference: Http://www.lecloud.net/post/3378834922/install-redis-php-extension-phpredis-with

The following code is the reference site installation method, please adjust 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.ini if Extension_dir is not configured, configure the path to your extension CP redis.so/usr/local/lib/php5/extension/join a line: Extension = redis.so Restart Nginx and php-fpm to output phpinfo to see Redis extension installation Complete

Set PHP.ini

Find Session.save_handler = Files This line, modified to Session.save_handler = Redis, the original session storage method to micro-redis way to improve speed, The reason is that Redis is a bit faster in memory reading than in file reads

Find Session.save_path = "/tmp" this line, modified to Session.save_path = "tcp://127.0.0.1:8888", which is pointing to the local TCP service, this service will be launched by our Redis

Restart Nginx and PHP-FPM output phpinfo can see the session save way and path has changed

Start Redis Service

Redis-server--port 8888 &

Writing PHP code

 
  ' Fanqie ', ' CCC ' = ' Hello Redis '); $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 8888); Echo ' sessionid>> >>>>> phpredis_session: '. SESSION_ID (); Echo '
'; Echo '
';//redis uses session_id as key and is a string to store echo ' using Redis for >>>>>>> ' via PHP. $redis->get (' Phpredis_session: '. SESSION_ID ()); Echo '
'; Echo '
' Echo ' gets >>>>>>> with session via PHP
'; Echo '
'; Var_dump ($_session[' test_session '); Echo '
';

Page output, we can see that we have successfully stored and successfully acquired the stored value through Redis

Sessionid>>>>>>> phpredis_session:fmd75d3dfrlkp43eh567f7dsi5 via PHP with Redis get >>>> >>>test_session|a:2:{s:4: "Name"; s:6: "Fanqie"; s:3: "CCC"; s:12: "Hello Redis";} Get >>>>>>>array (2) {  ["name"]=>  string (6) "Fanqie"  ["CCC"] with the session via PHP]=>  string (+) "Hello Redis"}

Use the Redis client to see that a record of Hello Redis has been stored in the Redis library

dangcheng@dangcheng-pc:~$ redis-cli-p 8888127.0.0.1:8888> Select 0ok127.0.0.1:8888> keys *) "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.