Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Stanimir Dimitrov for ZF2 authentication session storage in memcached

public function initSession()
{
    $sessionConfig = new SessionConfig();
    $sessionConfig->setOptions([
        'cookie_lifetime'     => 7200, //2hrs
        'remember_me_seconds' => 7200, //2hrs This is also set in the login controller
        'use_cookies'         => true,
        'cache_expire'        => 180,  //3hrs
        'cookie_path'         => "/",
        'cookie_secure'       => Functions::isSSL(),
        'cookie_httponly'     => true,
        'name'                => 'cookie name',
    ]);
    $sessionManager = new SessionManager($sessionConfig);
    // $memCached = new StorageFactory::factory(array(
    //     'adapter' => array(
    //        'name'     =>'memcached',
    //         'lifetime' => 7200,
    //         'options'  => array(
    //             'servers'   => array(
    //                 array(
    //                     '127.0.0.1',11211
    //                 ),
    //             ),
    //             'namespace'  => 'MYMEMCACHEDNAMESPACE',
    //             'liboptions' => array(
    //                 'COMPRESSION' => true,
    //                 'binary_protocol' => true,
    //                 'no_block' => true,
    //                 'connect_timeout' => 100
    //             )
    //         ),
    //     ),
    // ));

    // $saveHandler = new Cache($memCached);
    // $sessionManager->setSaveHandler($saveHandler);
    $sessionManager->start();
    return Container::setDefaultManager($sessionManager);
}

This is the function I use in order to create a cookie for X user. The cookie lives for 3 hours, no matter if there are redirects or if the user has closed the browser. It's still there. Just call this function in your onBootstrap() method from Module.php.

While logging, I use The ZF2 AuthenticationService and the Container to store and retrieve the user data.

I suggest you to install these module for easier debugging. https://github.com/zendframework/ZendDeveloperTools https://github.com/samsonasik/SanSessionToolbar/


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>