简体   繁体   中英

PHP FPM Docker ZF1: The session has already been started. The session id must be set first

In my dev environment, I'm trying to replace the old heavy Vagrant VM with Docker, using docker-compose :

version: '2'
services:
    nginx:
        build: ./containers/nginx
        networks:
            mm:
                ipv4_address: 172.25.0.101
        environment:
            APPLICATION_ENV: development
        extra_hosts:
            - "mysite.dev:127.0.0.1"
        ports:
            - 80:80
        links:
            - php
        volumes:
            - ../:/srv

    php:
        build: ./containers/php-fpm
        networks:
            mm:
                ipv4_address: 172.25.0.102
        volumes:
            - ../:/srv
        links:
            - memcached
        ports:
            - 9000:9000

    memcached:
        image: memcached:latest
        ports:
            - 1234:11211
        networks:
            mm:
                ipv4_address: 172.25.0.103

networks:
    mm:
        driver: bridge
        ipam:
            driver: default
            config:
                - subnet: 172.25.0.0/24

But the application is written with ZF1, and whenever I instantiate an Zend_Session_Namespace object just after Zend_Session::start() the following error appears:

Zend_Session_Exception: The session has already been started. The session id must be set first. in /srv/mm/vendor/zendframework/zendframework1/library/Zend/Session.php on line 667

I've tried it all, mouting a session path with VOLUME , changing the session.save_path , installing it all again and again, and nothing happens.

After spent a long debugging this issue I've found the answer for your question. On the second time you call Zend_Session::start() ZF will validate the session id by checking whether it was generated with proper hash_bits_per_character defined by php_ini setting session.hash_bits_per_character or not.

/**
 * From Zend_Session 
 */
protected static function _checkId($id)
{
    $saveHandler = ini_get('session.save_handler');
    if ($saveHandler == 'cluster') { // Zend Server SC, validate only after last dash
        $dashPos = strrpos($id, '-');
        if ($dashPos) {
            $id = substr($id, $dashPos + 1);
        }
    }

    $hashBitsPerChar = ini_get('session.hash_bits_per_character');
    if (!$hashBitsPerChar) {
        $hashBitsPerChar = 5; // the default value
    }
    switch($hashBitsPerChar) {
        case 4: $pattern = '^[0-9a-f]*$'; break;
        case 5: $pattern = '^[0-9a-v]*$'; break;
        case 6: $pattern = '^[0-9a-zA-Z-,]*$'; break;
    }
    return preg_match('#'.$pattern.'#', $id);
}

The problem is that for some reason the session id is generated with hash_bits_per_character = 5 instead of hash_bits_per_character = 4 . So, to solve this problem, all you need to do is force the session.hash_bits_per_character setting before calling Zend_Session::start() for the first time and Zend_Session will check the hash bits using the correct pattern:

Zend_Session::setOptions(['hash_bits_per_character' => 5]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM