Prevent non-logged users from accessing pages

Asked

Viewed 75 times

2

I have a class called "Account" and its methods are the pages that the logged-in user can access, e.g.:

Class account {

function info (){
//bla bla
}
function post () {
// bla bla
}

}

and for each method the user accesses, I have to make a conditional to check if his session is active, ex:

if($userLogged){
//deixar acessar
} else {
//redireciona
}

Is there any way to do a single check for all methods? something like a Construct, for any method that was instantiated the check would already be done and tried to give access or redirect the user.

UPDATING:

Is it safe to make a simple Construct for this? ex:

function __construct() {

        if (!$userLogged)
            die('blabla');
    }

1 answer

0

Try using the magic method: __call that is activated every time you try to access some class function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.