Let PHP Session into Redis configuration method

Source: Internet
Author: User
Tags php session phpinfo 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 install Cd/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

    <?php header ("Content-type:text/html;charset=utf-8"); Session_Start ();//This is important $_session[' test_session ']= @array (' name ' = ' fanqie ', ' CCC ' = ' Hello Redis '); $redis = new Redis (), $redis->connect (' 127.0.0.1 ', 8888); Echo ' sessionid>>>>>>> phpredis_ SESSION: '. SESSION_ID (); Echo ' <br/> '; Echo ' <br/> ';//redis uses session_id as key and is a string to store the echo ' Redis get > through PHP >>>>>> '. $redis->get (' phpredis_session: '. session_id ()); Echo ' <br/> '; Echo ' <br/> ' Echo ' Get >>>>>>><br/> ' via PHP, Echo ' <pre> '; Var_dump ($_session[' Test_ Session ']); Echo ' </pre> ';

    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

    [Email protected]:~$ 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>

Let PHP Session into Redis configuration method

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.