We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

redirecting pages if you have not login

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

<?php session_start(); //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.phalcon.io/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.phalcon.io/en/latest/reference/dispatching.html The INVO tutorial which has some practical examples: https://docs.phalcon.io/en/latest/reference/tutorial-invo.html



81.2k

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