Censorship of Page not logged in

Asked

Viewed 70 times

0

I’m having a problem when it comes to putting a block page...

When I put the block at the top of the site, that case was using it:

    if(!empty($_SESSION['id'])){
    echo "Olá ".$_SESSION['nome'].", Bem vindo <br>";
    echo "<a href='sair.php'>Sair</a>";
}else{
    $_SESSION['msg'] = "Área restrita";
    header("Location: login.php");  
}

He gives me that mistake: headers already sent

Because of that line on my page:

> if(mysqli_affected_rows($conn)){      $_SESSION['msg'] = "<p
> style='color:green;'>Item apagado com sucesso</p>";
>       header("Location: adm_tabela.php"); **//Essa Linha que dá o erro**

How to make her lock so that no error ?

  • 1

    It is because you are making two redirects. You have to finish the script on else... Try putting a die(); after the line header("Location: login.php"); in the else

  • 1

    Related: https://answall.com/q/4251/23400

1 answer

1


The problem is why you’re doing it header: Location: ... twice, since the script is not interrupted when it should (at the end of the else, since the intention is to redirect the user without permission to the login page).

In this answer has more details, and in this excerpt is the description of the problem:

The/output page always follows the headers. PHP is required to pass the headers to the server first. He can only do this once. And after the double line break (sending output to simplify), he can’t add more headers. (Grifei)

To fix just do exit or die at the end of else, so the redirect will occur and the script will be stopped:

if (!empty($_SESSION['id'])){        
    echo "Olá ".$_SESSION['nome'].", Bem vindo <br>";
    echo "<a href='sair.php'>Sair</a>";
}

else {
    $_SESSION['msg'] = "Área restrita";
    header("Location: login.php"); 
    exit();
}

Browser other questions tagged

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