Skip to content

Instantly share code, notes, and snippets.

@askaidau
Created October 10, 2016 08:46
Show Gist options
  • Save askaidau/1f0afb34edcbda86b9a9c7c9b859b501 to your computer and use it in GitHub Desktop.
Save askaidau/1f0afb34edcbda86b9a9c7c9b859b501 to your computer and use it in GitHub Desktop.
PHP 7 Memcached session_regenerate_id() E_RECOVERABLE_ERROR Fix
<?php
// affects (but not limited to) stock PHP 7.0x on Ubuntu 16.04.3
// source: https://bugs.php.net/bug.php?id=71187
// assuming we are using memcached for session store
ini_set("session.save_handler", "memcached");
ini_set("session.save_path", "localhost:11211");
// we need to extend and override the read method to force it to return string value
// in order to comply with PHP 7's more strict type checking
class MemcachedSession extends SessionHandler
{
public function read($session_id) {
return (string)parent::read($session_id);
}
}
$sess = new MemcachedSession();
session_set_save_handler($sess, true);
session_start();
session_regenerate_id();
// doing this will no longer trigger this fatal error:
// PHP Catchable fatal error: session_regenerate_id(): Failed to create session ID: memcached (path: 127.0.0.1:11211) in /var/www/vhosts/advancedtomato.com/httpdocs/file.php on line 143
@askaidau
Copy link
Author

By way of an example, this quickfix snippet can be included anywhere prior to your session_start() calls:

class MemcachedSession extends SessionHandler
{
    public function read($session_id) {
        return (string)parent::read($session_id);
    }
}

if( ini_get("session.save_handler")=="memcached" ) {
    $sess = new MemcachedSession();
    session_set_save_handler($sess, true);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment