# Custom Session handler doesn't work

**URL:** <https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126>\
**Category:** Components & MVC\
**Created:** [May 24, 2017, 7:09am UTC](https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126 "2017-05-24T07:09:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitriy\_Stelmakh](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dmitriy\_Stelmakh](https://discourse.laminas.dev/u/Dmitriy_Stelmakh)\
**Post date:** [May 24, 2017, 7:09am UTC](https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126/1 "2017-05-24T07:09:28Z")

</div>

I’m trying to use custom session handler using Google Datastore. There is a part of code of main module:

```
 public function onBootstrap(MvcEvent $e)
 {
    $sessionConfig = new SessionConfig();

    $sessionManager = new SessionManager($sessionConfig);

    $sessionConfig->setOptions(
        $e->getApplication()->getServiceManager()->get('Config')['datastore-session']['session']
    );

    $sessionsStorage = $e->getApplication()->getServiceManager()->get('CustomSessionStorage\Storage\CustomStorage');
    $sessionManager->setSaveHandler($sessionsStorage->createHandler());

    $sessionManager->start();

    Container::setDefaultManager($sessionManager);
}

```

But it doesn’t work. Sessions are still using files on server and method “SessionHandlerInterface::open” is never called.

But when I write this code (in the same place onBootstrap), session handler is used and methods “open”, “read” etc is used. Why the first code doesn’t work?

```
session_write_close();
session_set_save_handler($sessionsStorage->createHandler(), true);
session_save_path('sessions');
session_start();

```

How to use my custom handler?

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Stelmakh](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dmitriy\_Stelmakh](https://discourse.laminas.dev/u/Dmitriy_Stelmakh)\
**Post date:** [May 24, 2017, 8:24am UTC](https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126/2 "2017-05-24T08:24:43Z")

</div>

I’ve added logging in SessionManager::start [https://github.com/zendframework/zend-session/blob/master/src/SessionManager.php#L114](https://github.com/zendframework/zend-session/blob/master/src/SessionManager.php#L114)  
I’ve got the following [https://gist.githubusercontent.com/dmitriytestapic/6d8034bdf5ee1cef37359bd1d6bbea70/raw/63787c92581bc352801ae9071c1457c416a200fb/zf2\_session.log](https://gist.githubusercontent.com/dmitriytestapic/6d8034bdf5ee1cef37359bd1d6bbea70/raw/63787c92581bc352801ae9071c1457c416a200fb/zf2_session.log)

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [May 30, 2017, 6:49pm UTC](https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126/3 "2017-05-30T18:49:45Z")

</div>

Since you’re manually creating your `SessionManager` instance anyways… have you tried this:

```php
$sessionManager = new SessionManager(
    $sessionConfig,
    null,
    $sessionsStorage->createHandler()
);

```

My suspicion is that the default PHP save handler is getting initialized somehow during `SessionManager` construction, so if you can inject the new handler then, it may work.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Stelmakh](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dmitriy\_Stelmakh](https://discourse.laminas.dev/u/Dmitriy_Stelmakh)\
**Post date:** [June 2, 2017, 1:02pm UTC](https://discourse.laminas.dev/t/custom-session-handler-doesnt-work/126/4 "2017-06-02T13:02:52Z")

</div>

Unfortunately it didn’t help (
