Force Page Login - ZingPHP/Zing GitHub Wiki

How to force a login on a page

Many times you will require users to be logged in to see any action, you can force this by using the requireLogin() method from the User Module and placing it in the runBefore() method.

By creating this class in your Websites directory and going to http://example.com/users you will be redirected to a login page that if exists will ask for login credentials. Otherwise you will see the message in the main() method.

Websites/Users.php
class Users extends Zing{

    public function runBefore(){
        // Anything that uses this page will require the user to be logged in
        $this->user->requireLogin("/home/login");
    }

    public function main(){
        echo "<p>If you see me you are logged in!</p>";
    }

}

Note: User::requireLogin() will not work without using User::login() when logging a user in, as it sets extra session data to check if a user is logged in.

Extra Reading: Logging a User In