Search code examples
phpsessionslim

Slim App Session - session_start() works on one page but returns false on another


I have a couple of different routes, both use sessions. Locally everything works just fine but on my server the session_start in the second code chunk below returns false... Nothing in the server error logs either.

Excerpt from my file classes.php (works just fine)

// Get all classes
$app->get('/classes', function (Request $request, Response $response) use ($app) {
    session_start();

    // Make sure the person is logged in.
    if( !isset($_SESSION['user_id']) ){
        echo '{"type": "danger","text": "You must be logged in to access this page."}';
    return;
    }

    $userid = $_SESSION['user_id'];

    ...

Excerpt from my file profile.php (does not work, returns false on session_start() - files are in the same folder and referenced the same way)

// Get Profile
$app->get('/profile', function (Request $request, Response $response) use ($app) {
    session_start();

    // Make sure the person is logged in.
    if( !isset($_SESSION['user_id']) ){
        echo '{"type": "danger","text": "You must be logged in to access this page."}';
        return;
    }

    $userid = $_SESSION['user_id'];

    ...

Thanks for your help, any ideas about how to debug would be great!


Solution

  • Put session_start() call to index.php before any output sent to browser.