Make login system go back to previous page in php

2

I would like to make a system that the user when trying to access a url that needs to be logged in the script checks if it returns false it redirects to the login page and after logging in it automatically returns to the url that it was trying to access, how can i do this in php?

    
asked by anonymous 21.07.2014 / 22:53

2 answers

1

You can use Session to control whether the user is logged in or not, here's an example of how you could do this control.

PHP

class auth
{
    public static function logged($url)
    {
        if(!$_SESSION['logged'])
            header("location :".$url);          
    }

    public static function login($username, $password)
    {
        if($username == "usuario" && $senha == "senha")
            $_SESSION['logged'] = true;
        else
            return false;

        return true;
    }

    public static function logout()
    {
        if($_SESSION['logged'])
            unset($_SESSION['logged']);
    }   
}

And to make the control on your page do this.

PHP

auth::logged("/login");

You may be using this authentication class to mount the process and adapt to your system.

    
21.07.2014 / 23:08
0

I did not try it any more, I suggest doing something along those lines:

page that requires login.php:

if (!$_SESSION["logado"]) redirecionar_para_login();

login.php

if (loginSucesso()){
    $pattern = "pt.stackoverflow.com";
    //verifica se foi direcionado de alguma outra página sua
    if(preg_match("/$pattern/i", $_SERVER['HTTP_REFERER']){
        redirecionar_paginaAnterior();
    }
    else{
        redirecionar_paginaPrincipal();
    }
}
redirecionar_paginaAnterior(){
    header("Location: " + $_SERVER['HTTP_REFERER']);
    die();
}
    
21.07.2014 / 23:09