How to Connect to Redis Database via PHP

Posted on October 19, 2018 at 2:13 pm

First make sure to install PHP extension for Redis:

https://github.com/phpredis/phpredis

On Debian-like OS you can install it this way:

apt-get install php7.0-redis

Then you can connect to Redis via PHP this way:

$redis = new Redis(); 
 
try 
{
    $connected = $redis->connect('127.0.0.1', 6379, 1);
}
catch(Exception $e) 
{
    echo "Failed to connect to Redis database: ".$e->getMessage();
 
    return;
}
 
if(!$connected)
{
    echo "Failed to connect to Redis database";
 
    return;
}
 
$redis->auth('password');
 
$redis->set('key', 'value');

As you can see we handle connection issues.

Receive updates via email

Other Posts

Updated Posts