Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Sessions Not Seeming to Work

So I've got the default Phalcon setup, with my services.php file looking like this:

use Phalcon\Session\Adapter\Files as SessionAdapter;

//  ... Other Services Code here


$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

And my controller looks like this:

<?php

use Phalcon\Mvc\Controller;

class AdminController extends Controller
{

    public function indexAction()
    {
      if ($this->request->isPost() && $this->request->getPost("password") === "thePassword")
      {
        $this->session->set("adminLogin", true);
      }

      if($this->session->has("adminLogin") && $this->session->get("adminLogin"))
      {
        $this->view->pick("admin/admin");
      }
      else
      {
        $this->view->pick("admin/login");
      }
    }
}

For a very basic, password-protected admin screen. However, it always kicks me back to the login page, even if the password is correct.

I've tried just putting in this:

      $this->session->set("adminLogin", true);
      var_dump( $this->session );

And I get this as output:

object(Phalcon\Session\Adapter\Files)#33 (3) { ["_uniqueId":protected]=> NULL ["_started":protected]=> bool(false) ["_options":protected]=> NULL }

I tested just setting a variable inside the password checking block and then checking for that variable on the admin view block, and that works, so the problem is with my sessions.

Your code looks pretty straightforward. Rather than calling var_dump() on $this->session, try calling it on $_SESSION, to see what that looks like. Maybe try adjusting your code to talk to $_SESSION directly - at least on a temporary basis to see if you can find the code that's causing the error.



1.8k
Accepted
answer
edited Aug '15

It worked using $_SESSION, so I kept messing around and found out that the original code wasn't even creating a session cookie for the client, and that to get the Phalcon Sessions to work, I needed to add session_start(); to my code. Wasn't expecting it to be necessary for Phalcon sessions, but I suppose since they pretty much just use PHP's normal sessions that makes sense.

So in the end it works when my controller looks like this:

<?php

use Phalcon\Mvc\Controller;

class AdminController extends Controller
{

    public function indexAction()
    {
      session_start(); //Only part changed from the original code.
      if ($this->request->isPost() && $this->request->getPost("password") === "thePassword")
      {
        $this->session->set("adminLogin", true);
      }

      if($this->session->has("adminLogin") && $this->session->get("adminLogin"))
      {
        $this->view->pick("admin/admin");
      }
      else
      {
        $this->view->pick("admin/login");
      }
    }
}