In a PHP project, use Redis to save sessions
Introduction In a PHP project, the default session saving is file (file). When the number of users is large, the file reading will be slower. Using Redis to save the session can greatly improve the efficiency of session usage.
By default, PHP supports using Redis to save sessions. It does not require any other extended class library or code to implement it. You only need to change the configuration in php.ini.
The command used by Redis to save the session is setex, which can be detected by real-time monitoring with redis-cli monitor.
Redis saves the session using the syntax of the setex command:
1 2 | # key is the key value, time is the expiration time, value is the value of the string type setex(key, time , value) |
1. Redis environment
1. Make sure the Reids environment is installed and configured into the PHP project
2. The php redis extension is installed in php, and the redis extension can be seen through the function phpinfo(), as shown below:
3. Test whether PHP can access redis normally, and refresh the page. If you see the number increasing, it means that the Redis environment is normal. The test code is as follows:
1 2 3 4 5 6 7 | <?php $redis = new Redis(); $redis ->connect( '127.0.0.1' , 6379); $count = $redis ->exists( 'count' ) ? $redis ->get( 'count' ) : 1; echo $count ; $redis ->set( 'count' , ++ $count ); |
Second, configure PHP
After installing the Redis extension, you will see the session support handle in the phpinfo() output, which includes redis, as follows:
One thing to note is that the maximum expiration time of Redis is 2147483647, which is 7fffffff in hexadecimal. Therefore, the value of the recovery time session.gc_maxlifetime in the session configuration cannot exceed this number. Otherwise, when Redis uses the setex command to save the session, the expiration time is a negative number, which is equivalent to directly deleting the key. Not only will this not save successfully, PHP will also return the following error:
1 | Warning: Unknown: Failed to write session data (redis). Please verify that the current setting of session.save_path is correct (127.0.0.1:6379) in Unknown on line 0 |
1. Modify php.ini
Open the php.ini file and set the following two values:
1 2 | session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379" |
If Redis has a password, use the following configuration:
1 2 | session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379?auth=password" |
After the configuration is complete, restart php-fpm.
2. Modify php-fpm.conf
The PHP-FPM configuration file /etc/php-fpm.conf or /etc/php-fpm.d/*.conf also has session configuration.
Their session configuration has a higher priority than php.ini and will override the configuration in php.ini.
So, change it directly here:
1 2 | php_value[session.save_handler] = redis php_value[session.save_path] = "tcp://127.0.0.1:6379" |
After the configuration is complete, restart php-fpm.
3. PHP code configuration
Sessions can also be configured in running PHP code as follows:
1 2 3 4 | <?php ini_set ( 'session.save_handler' , 'redis' ); ini_set ( 'session.save_path' , 'tcp://127.0.0.1:6379' ); |
3. Finally, in the PHP project, use Redis to save the session test, and test the code:
1 2 3 4 5 6 7 8 9 10 | <?php // If you refresh the page and see that the number keeps increasing, it means that Redis is configured to save the session successfully.。 session_start(); $count = isset( $_SESSION [ 'count' ]) ? $_SESSION [ 'count' ] : 1; echo $count ; $_SESSION [ 'count' ] = ++ $count ; |
0 Comments