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://127.0.0.1:6379)
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.
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.
git clone https://github.com/DragonBe/docker-php-redis-example.git
We’ve added a docker-compose.yml to get started quickly using Docker Compose.
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.
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.
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.