Check if URL is different in PHP

0

In JavaScript, we can check if the url is different, we have window.location.href

There you go:

var urlAtual = window.location.href;
var urlCadastro = "www.teste.com/cadastro";
if(urlAtual != urlCadastro){
  window.location.href = "www.teste.com/login";
}

What have I tried, would it work?

$atacadoLogado = $this->helper('customer')->isLoggedIn();
$urlDoCadastro = "https://www.meusite.com/cadastro";
$pegarDominio = $_SERVER['HTTP_HOST'];
$urlAtualizada = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $pegarDominio . $_SERVER['REQUEST_URI'];
if(!$atacadoLogado && $urlAtualizada != $urlCadastro){
    header("Location: https://www.meusite.com/login");
}
    
asked by anonymous 17.07.2017 / 16:35

2 answers

3

You can use the super global $ _SERVER

$server = $_SERVER['SERVER_NAME'];
$endereco = $_SERVER ['REQUEST_URI'];

 $clienteLogado = $this->helper('customer')->isLoggedIn();
$urlCadastro = $server.$endereco
if(!$atacadoLogado && !$urlCadastro){
    header("Location: http://www.meusite.com/login");
}

In this link you find all indexes and the function of each one on this super variable $ _SERVER.

    
17.07.2017 / 16:41
2

Yes, my friend, let's go to an example.

$url = "https://www.google.com.br";
//Aqui ira pegar o dominio
$dominio= $_SERVER['HTTP_HOST'];
//Aqui ira concatenar com o http:// ou https:// e salvar em url

 $urlAtual = (isset($_SERVER['HTTPS']) ? "https" : "http")."://".$dominio. $_SERVER['REQUEST_URI'];
//Verifica se é diferente
if($url != $urlAtual){

echo "Diferente";
}
else{
echo "Não diferente";
}
    
17.07.2017 / 16:41