Skip to main content

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second…
With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path.

Our challenge

One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis.
Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem.
Warning: session_start(): Failed to read session data: redis (path: tcp://
Searching the internet for this error, we didn’t got many hits that could point us into a direction of a solution. 
We came up with this very basic PHP code snippet that could reproduce the error in a clean, stand-alone way. Since we used a framework, we wanted to make sure the problem was in PHP and not related to the choice of our PHP framework.

ini_set('session.save_handler', 'redis');
    'session.save_path', 'tcp://devcache1:6379'

echo nl2br(
    '<pre>' . session_save_path() . '</pre>' . PHP_EOL

echo nl2br(
    'Running PHP version: ' . phpversion() . PHP_EOL

if (!array_key_exists('visit', $_SESSION)) {
    $_SESSION['visit'] = 0;

echo nl2br(
    'You have been here ' . $_SESSION['visit'] . ' times.'
To give us some room to try things out, we decided to use Docker to create containers for us to try things out while searching for a solution. Find our basic setup from my GitHub repository.
cd /path/to/workspace
git clone
cd docker-php-redis-example/
We’ve added a docker-compose.yml to get started quickly using Docker Compose.
docker-compose up
This command will launch the different containers for PHP 5.6, 7.0 and 7.1 including a single Redis instance.
In the following YouTube video you can see what we experienced on our side.
PHP7, Redis extension and Docker
The redis PHP session handler worked perfect for PHP 5.6, failed with Segmentation faults on PHP 7.0 and wasn’t able to initialise a new session on PHP 7.1. Remarkable was that once we started a session on PHP 5.6, the session handler was having no issues updating this existing session on PHP 7.1.

Our solution

So we figured out there must be something broken with the update in the redis extension. Looking at the changelog we couldn’t directly spot a problem on how sessions are instantiated. Searching the internet we came to a lot of mentioning of session_regenerate_id(), but this was not affected in our situation.
In one of the comments someone mentioned that a similar failure started appearing after upgrading from v3.0.0 to v3.1.0. That sparked our interest. We downgraded our redis extension to version 3.0.0 and tried again.
Hooray!!! Our session creation was now working on PHP 7.1, so mission accomplished. Now we needed to agree on taking the unfixed issues between redis-3.0.0 and redis-3.1.0 as compromise. Since the earlier mentioned changelog didn’t contain issues that directly could have an effect on our use cases for Redis it was easy to agree.
Now we’re enjoying running our web applications on PHP 7.1.0 while our sessions are handled through Redis. And we hope by sharing this experience, you too can enjoy running your PHP application on the latest version: PHP 7.1.

Update 2016-12-22!!!

Today I received a tweet that there's an update available in the form of redis-3.1.1RC1. Go check out my github branch to see how to implement it with Docker.

This fix also solves the issue I displayed for PHP 7.0. You should be good to go and implement it on your PHP 7 installations.


Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL" Set up for in…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…