Sessions not persisting past initial login redirect in v1.5
Before I get into this I'll say that I'm guessing the problem here is with my code rather than Slim itself, but having spent several hours on this already I'm rather stumped:
Just updated a new project to v1.5 of Slim, and made (what I know to be) all the appropriate changes needed, including the following:
- declaring a new instance of Slim for my app
- replacing all instances of Slim::method() with $app->method()
- getting (when needed) an instance of my app via Slim::getInstance()
Things appear to be basically working now, but whenever I login to my app, my session never persists past the initial login redirect. Whenever trying to navigate to another page, I'm kicked out back to the original login page.
I have a single master template file that loads partials. Before loading the partials it checks for a logged in user like so:
if ( isset($_SESSION['loggedIn']) and $_SESSION['loggedIn'] == TRUE and setupIsComplete() ) { $app->render($this->data['panelToDisplay']); } else { if ( !setupIsComplete() ) { if ( $resourceUri !== "/" ) { $app->redirect('./../setup/'); } else { $app->redirect('./setup/'); } } else { if ( $resourceUri !== "/" ) { $app->redirect('./../'); } else { $app->render('admin/login.php'); } } }
The login method that actually sets the session variables is this (I've left out the various checks and included just the relevant portion):
if ( App_User::encryptPassword( $password ) === $row['password'] ) { if ( !session_id() ) { session_start(); } $_SESSION['loggedIn'] = TRUE; $_SESSION['username'] = $username; $_SESSION['userID'] = $row['id']; $_SESSION['role'] = $row['role']; } else { session_destroy(); unset($_SESSION['loggedIn']); unset($_SESSION['username']); unset($_SESSION['userID']); unset($_SESSION['role']); $app->flashNow('notification', "The username or password entered was incorrect. Please check that your caps lock key is not on."); }
The POST route that handles the actual login is like so:
//POST Login the specified user $app->post('/', 'login'); function login() { $app = Slim::getInstance(); if ( $app->request()->params('username') === '' || $app->request()->params('password') === '') { $app->flashNow('notification', 'Please fill in both fields to login.'); } else { App_User::loginUser( $app->request()->params('username'), $app->request()->params('password') ); } $app->render( 'admin/index.php', array( 'pageTitle' => 'Admin Dashboard', 'panelToDisplay' => 'admin/dashboardPanel.php', 'currentTab' => 'dashboard', 'adminView' => 'TRUE' ) ); }
Regarding the code above (all snippets) I've replace my actual application name with "app", but otherwise left it unchanged. I'm sure there is much about this code that could be improved, as I'm still fairly new to PHP, but at this point I'd be happy just to get this working properly.
As mentioned at the beginning, I'm assuming the problem is with my code somewhere, but just on the off chance this is Slim-related I've submitted it here.
Any thoughts? Many thanks~
Jonathan
Does your browser accept third-party cookies? Session data is stored in an encrypted cookie by default.
Yes. I've tried in all major browsers, IE8/9, Chrome, Firefox, and Safari, and double-checked that each browser has (EDIT: accepts 3rd party) cookies enabled.
I've been working on this (in my own app) for the past couple of days without much progress. I then created a completely separate, very simple Slim 1.5 app that does just the following:
- Display a login page
- After logging in, redirect to another page
- The first page shown after login checks if the user is logged in, and displays a link to a second page
- The second page also checks if the user is logged in, and displays a link to the first page
Here's a link to the live app on our site: http://test.nimblehost.com/slim15/ username: admin password: test
You can download the source for this simple app here: http://test.nimblehost.com/simpleLogin-Slim1.5.zip
At this point, barring some really stupid mistake on my part, this appears to be a Slim issue. I've gone through Slim's source to try and pinpoint where the problem might be, but haven't found anything so far.
Insert session_write_close() immediately before $app->redirect(). This will force sessions to be serialized and written out before the PHP script ends. Does this work?
That does indeed work, both in my app, and in the simple login app on our test server. Thanks!
On a side note, is this something I should be doing in my own code (best practice?) or will it be incorporated into Slim?
This is a regression that will be fixed very soon in Slim. For now, hard-code it in your app. The next point release will include this fix.
Roger that. Thanks again!
Hey,
I'm not sure if this is fixed, but how would cnames affect this? We are using the SessionCookie middleware and if we do a redirect in another custom middleware, the session on redirect is lost. but if we switch to native sessions everything works fine.
We are also using a SessionCookie middleware and are experiencing the same issue with our Cookie Session being lost on a redirect.
Hi i have same problem with last Slim version downloaded from website, and session_write_close(); not working :(. I think i know what it is, i save in session multy array, and if it is too big or something, session is lost
This issue is still valid for ~v2.6.