9951 explained code solutions for 126 technologies


predisHow can I use Redis to store and retrieve PHP session data?


Redis is an in-memory data structure store that can be used to store and retrieve PHP session data. It is a fast and reliable way to store session data.

To use Redis for storing and retrieving PHP session data, you first need to install the Redis PHP extension.

$ sudo apt-get install php-redis

Once the extension is installed, you can configure PHP to use Redis for session storage. To do this, you need to add the following line to your php.ini file:

session.save_handler = redis

You also need to specify the Redis server host and port in the php.ini file:

session.save_path = "tcp://hostname:port?database=0"

Once the configuration is complete, you can use the session_start() function to start a new session and the session_id() function to get the current session ID. You can use the $_SESSION superglobal to store and retrieve data from the session.

For example:

<?php
session_start();
$_SESSION['name'] = 'John Doe';
echo session_id(); // Outputs a unique session ID
?>

Helpful links

Edit this code on GitHub