redirecting pages if you have not login

Previously I copied this code on each form. Is this the correct way to work Phalcon ?

<?php sessionstart(); //manejamos en sesion el nombre del usuario que se ha logeado if (!isset($SESSION["usuario"])){ header("location:../../index.php?nologin=false"); }?>

No, you do not have to handle sessions, phalcon does that for you. Just add session to your DI in your startup file like so:

    // session
    $di->setShared('session', function() {
        $session = new \Phalcon\Session\Adapter\Files();
        $session->start();
        return $session;
    });

After that you can read more here: https://docs.phalconphp.com/en/latest/reference/session.html Section "Storing/Retrieving data in Session"

if ($this->session->has("YOUR_SESSION_NAME_HERE")) { 
    // Logged in
} else {
    // Not logged in. Redirect or do something...
}

The best solution would be to create a new eventsManager and attach it to the Dispatcher. In that eventsManager, define a method beforeDispatch that checks if a user is logged in. If not, redirect back to the login page. This way you don't have to check if the session exists at the beginning of every action.

More info on dispatching controllers: https://docs.phalconphp.com/en/latest/reference/dispatching.html The INVO tutorial which has some practical examples: https://docs.phalconphp.com/en/latest/reference/tutorial-invo.html



34.2k

Okay , I'll do the tests and told them anything