php session redis configuration

I believe that many people have used databases, Memcache and file storage sessions, so can Rdis store sessions? The answer is yes.
Specific environment:
an apache+php server (yum installs remi source and configures httpd-2.2.15 php-5.4.45)
a redis server (yum installs remi source and configures redis-3.2.6) to
ensure that the apache server can be accessed Port 6379 of redis server
Specific steps:
1. Install redis extension on apache+php server

Click (here) to collapse or open

  1. yum install php-pecl-redis

2. Test whether the php extension is successfully installed
more index.php

Click (here) to collapse or open

  1. php
  2. phpinfo()
  3. ?>

The browser access is as follows:

As shown in the figure above, the installation is successful. The version number here is different from the version number of redis itself, so you can ignore it.
3. Then modify the php.conf file. Many people may wonder if it is not the modification of the php.ini file? I'll talk about it later.

Click (here) to collapse or open

  1. more /etc/httpd/conf.d/php.conf

Will be the following two lines:

Click (here) to collapse or open

  1. php_value session.save_handler "files"
  2. php_value session.save_path "/var/lib/php/session"

Amended as follows

Click (here) to collapse or open

  1. php_value session.save_handler "redis"
  2. php_value session.save_path "tcp://192.168.9.131:6379"

Here are some articles that add tcp, and some do not. I have tested that it can be stored in redis with or without session.
What I personally understand is: adding tcp means tcp protocol, if not adding it means http protocol. If it is not right, please do not spray~~
Restart the httpd service after saving and exiting
4. Write a test page test.php with the following content:

Click (here) to collapse or open

  1. header("Content-type:text/html;charset=utf-8");
  2. session_start();//This is very important
  3. $_SESSION['test_session']= @array('name' =>'fanqie' , 'ccc'=>'hello redis ');
  4. $redis = new redis();
  5. $redis->connect('192.168.9.131', 6379);
  6. echo 'sessionid>>>>>>> PHPREDIS_SESSION:' . session_id();
  7. echo '
    ';
  8. echo '
    ';
  9. //redis uses session_id as the key and is stored in the form of string
  10. echo'Get with redis through php >>>>>>>>'.$redis->get('PHPREDIS_SESSION:'. session_id());
  11. echo '
    ';
  12. echo '
    ';
  13. echo'Get through php with session >>>>>>>>
    ';
  14. echo '
    ';
    			
  15. var_dump($_SESSION['test_session']);
  16. echo '';

5、测试,通过浏览器访问

在redis里面查看是否有这个session

redis中的session值和浏览器访问的值一样,说明成功。
这里我说一下为什么我直接修改的php.conf文件,而不是php.ini文件
如果你两个都配置了,它们的优先级比较高的是php.conf文件。所以说如果你想配置php.ini文件,你还需要注释掉php.conf中默认存本地的方式。这样更复杂繁琐。相信大家会选择上面比较简单的方式。

更多前沿PHP技术请搜索千锋PHP,做真实的自己,用良心作教育

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/72638762