The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Cookie Session in middleware

Scott Nicolson's Avatar

Scott Nicolson

13 May, 2013 08:39 PM

I am trying to set a Session value using the super global $_SESSION in a middleware but it doesn't get set and saved.

I have invoked the Slim\Midddleware\SessionCookie after my custom middleware. I am basically setting a flag $_SESSION['flag'] = true and redirecting using $this->app->redirect('/'). after the redirect he session is still empty.

I have put var_exports in my middleware and the session middleware around the calls to load and session and get the following output:

// loading session debug message
// my middleware debug message
// saving session debug message

Any ideas where i might be going wrong?


  1. 1 Posted by Scott Nicolson on 13 May, 2013 09:39 PM

    Scott Nicolson's Avatar

    so i seem to be able to solve the problem by replacing the $this->app->redirect('/') with a $this->app->response()->redirect('/');

    I noticed this while i was using curl to debug further. The first redirect throws an exception. the second doesn't.

    Why does it throw an exception in the first case?

  2. 2 Posted by David Rodger on 15 May, 2013 01:09 AM

    David Rodger's Avatar

    So you're doing something like...

    $app->add( new \My\Middleware\Custom() );
    $app->add( new \Slim\Middleware\SessionCookie() );
    /** and later **/
    So if you write to $_SESSION in your middleware, it ought to be set and then saved. The session is loaded before \Slim::call() and saved after \Slim::call().

    Why does it throw an exception in the first case?

    I run through what I understand. You might already have looked at Slim's code and got much of this anyway.

    $app->redirect() calls\Slim::stop(). This is then caught in \Slim::call(), which writes any output emitted up to that point into the response object. After all middleware calls are made, \Slim::run() finalizes the response, and sends headers, including 'Location' which triggers to redirect in the browser.

    The sequence of call methods in middleware objects should not impede session writing. If you look at the SessionCookie middleware's call() method, it writes the session after any enclosed middleware calls. SO the sequence would be something like:
    1. Custom middleware call() is entered.
    2. SessionCookie middleware call() is entered. Session is loaded.
    3. Slim call()
    4. SessionCookie writes session to cookie. SessionCookie::call() is exited.
    5. Custom middleware call() is exited.

    On the face of it, there's nothing that should stop the session data being written to the cookie. Am I missing something?

  3. 3 Posted by Scott Nicolson on 15 May, 2013 07:38 AM

    Scott Nicolson's Avatar

    Thanks for the response David.

    I have added an example app that illustrates the issue. The app has one custom middleware that short circuits 2 routes. The '/middleware_fail' route is what i thought was strange.

    The repo can be found here.

    Hopefully this highlights the session data issue.
    I agree that on the surface of it that this should work. Let me know if it is not clear.

  4. 4 Posted by David Rodger on 16 May, 2013 11:49 AM

    David Rodger's Avatar

    I haven't tried your code, but I'm intrigued so I'll see if I can find time in the next couple of days.

    After looking at your code and at Slim, I really can't see a reason why \Slim\Slim::stop() and the way that Slim::call() catches the resulting Exception would prevent the SessionCookie middleware from saving, since the session saving occurs after any enclosed call() methods. In fact, calling Slim\Http\Response:redirect() doesn't throw the exception, so it's conceivable that Slim would continued running its call method. Either way, any code after the enclosed call() in any middleware should run.

    I wonder if there's something else going on.

    You needn't reply to this, as I haven't made any progress toward resolving the issue.

  5. 5 Posted by Joney on 13 Mar, 2016 01:19 PM

    Joney's Avatar

    We are in need of Mobdro for notebook to run correctly

  6. 6 Posted by JonLy on 30 Mar, 2016 06:51 PM

    JonLy's Avatar

    Easy to get Mobdro online tv here:

  7. 7 Posted by Mobdro apk on 04 Apr, 2016 07:53 PM

    Mobdro apk's Avatar

    download mobdro apk and app free just with one click.

  8. 8 Posted by Android app on 04 Apr, 2016 07:56 PM

    Android app's Avatar

    download free og youtube for watch best youtube videos on your phone.
    Movie tube is a best online tv app

  9. 9 Posted by williamgomez.ww on 05 Apr, 2016 05:38 AM

    williamgomez.ww's Avatar

    You can download movies by Vidmate App in your Android(APK), PC and iphone. you can download from this link:

  10. 10 Posted by Mary Prior on 06 Apr, 2016 06:24 AM

    Mary Prior's Avatar

    You can take help form this website:

  11. 11 Posted by JOhnMnry on 08 Apr, 2016 03:10 PM

    JOhnMnry's Avatar

    Here you can download Mobdro free

  12. 12 Posted by Mehna on 08 Apr, 2016 03:11 PM

    Mehna's Avatar

    Stream and download unlimited videos on your mobile phones

  13. 13 Posted by Apk Market on 20 Apr, 2016 10:03 AM

    Apk Market's Avatar
  14. 14 Posted by letsgoodsystems... on 27 Apr, 2016 02:18 PM

    letsgoodsystemsinfo's Avatar
  15. 15 Posted by David on 03 May, 2016 02:58 PM

    David's Avatar
  16. 16 Posted by Shane on 05 May, 2016 07:17 AM

    Shane's Avatar

    Nice for the informative article.

  17. 17 Posted by WilliamOa on 06 May, 2016 08:45 PM

    WilliamOa's Avatar

    Really enjoyed this article. Will read on...

  18. 18 Posted by kris on 24 May, 2016 11:59 AM

    kris's Avatar

    download vidmate apk

  19. 19 Posted by rahul on 25 May, 2016 07:02 AM

    rahul's Avatar

    haha thats cool blog download

  20. 20 Posted by rahul dev on 25 May, 2016 07:04 AM

    rahul dev's Avatar

    download cinemabox @

  21. 21 Posted by rebecca on 28 May, 2016 06:32 PM

    rebecca's Avatar

    I will make sure to bookmark it and come back to learn extra of your helpful info.
    itube apk

  22. 22 Posted by download vidmat... on 02 Jun, 2016 05:17 AM

    download vidmate's Avatar
  23. 23 Posted by lallu on 04 Jun, 2016 06:52 AM

    lallu's Avatar

    download vidmate

  24. 24 Posted by leonard on 19 Jun, 2016 12:47 PM

    leonard's Avatar

    Run the free android entertainment app for pc and tablets.

  25. 25 Posted by Samta Rani on 23 Jun, 2016 05:43 PM

    Samta Rani's Avatar
  26. 26 Posted by karam on 26 Jun, 2016 06:29 PM

    karam's Avatar
  27. 27 Posted by clashofclancoca... on 01 Aug, 2016 09:19 AM

    clashofclancocapk's Avatar

    Free Download Clash Of Clans APK.Dear COC APK Lover Here You Will Get All Clash Of Clans APK War Strategy,Loot Tips and Latest COC APK News.......

  28. 28 Posted by moroyifim2 on 13 Aug, 2016 10:45 AM

    moroyifim2's Avatar

    Can you show me how to create cookie redirect? The Amazing You Plan

  29. 29 Posted by Emily on 21 Aug, 2016 10:02 AM

    Emily's Avatar

    please help me i am not able to understand. <a href=""> Labor Day 2016 Images </a>

  30. 30 Posted by kohisicog4 on 30 Aug, 2016 01:19 AM

    kohisicog4's Avatar

    I have still have same problem and I still can't able to solve it. Eat Stop Eat review

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac