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.


Post a Comment

Popular posts from this blog

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-…

PHP Arrays - Associative Arrays or Hash Maps

Associative array or hash maps are listings of key and value pairs with a posibility to nest additional keys and values. An associative array is a very powerful construct within PHP.

In our previous article we discussed simple arrays, which in their turn are indexed associative arrays under the hood. Take the following example:

$array = [

Is in fact an indexed associative array under the hood:

$array = [
0 => 'apple',
1 => 'banana',
2 => 'chocolate',

But associative arrays can be so much more than just an indexed array, and you will find many database operations returning arrays where the fields of a table are the keys in the array while their values are also the values within the array.

$productRowData = [
'product_id' => 1234,
'brand_id' => 321,
'product_name' => 'Our awesome product',
'prodcut_description' => 'This is our most awesome product.&#…

Deploy Docker containers fast to Microsoft Azure

DEPLOY DOCKER CONTAINERS FAST TO MICROSOFT AZURE It’s hard to ignore the fact thatDockeris a way to move forward for rapid application development, distributed architectures and microservices. For developersDockeroffers great advantages as they can build their containers specifically for the task they work on. They grab a base image of a container, modify it for their purpose and prepare the functionality inside the container. Quality, testing and security teams now have a single instance to look at and ensure all functional and regulatory requirements are met. System engineers now don’t have to worry about providing a system with the required specs as the container is already provisioned for that purpose. But where do you deploy yourDockercontainers? You can set up your existing bare metal infrastructure to allow them to run containers, but this also means you need to learn about securing your container infrastructure, which is not an easy task. Luckily “the cloud” offers container …