Look, a recommendation. To set error messages, use sessions or even cookies. In addition to being viable for this task, they are easy to handle when it comes to storing and displaying simple error messages.
Example:
<?php
//-Simular banco de dados
$database = array("id" => 1,
"usuario" => "Edilson",
"password" => "palavrapasse",
"morada" => "Desconhecido_"
);
if(isset($_POST["cadastrar"])){
$nome = isset($_POST["nome"]) ? (string) $_POST["nome"] : NULL;
$password = isset($_POST["password"]) ? (string) $_POST["password"] : NULL;
if(in_array($nome, $database) && in_array($password, $database)){
setcookie("mensagem", "Login efectuado", time()+2);
//$_SESSION["mensagem"] = "Login efectuado";
// Sessoes do usuario...
// Outros
header("Location: logado.php");
exit();
} else {
//$_SESSION["erro"] = "Usuario nao existe";
setcookie("erro", "usuario nao existe", time()+2);
header("Location: erros.php");
exit();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{ Erros }</title>
</head>
<body>
<!-- Erros !-->
<div id="erros">
<?php if(isset($_COOKIE["erro"])){ echo $_COOKIE["erro"]; $_COOKIE["erro"] = NULL; } ?>
<?php //echo "ola"; ?>
</div>
<!-- Erros !-->
<form method="POST" action="">
<input type="text" name="nome" placeholder="Digite o nome"/><br/><br/>
<input type="password" name="password" placeholder="Digite senha"/><br/><br/>
<input type="submit" name="cadastrar" value="Cadastrar"/>
</form>
</body>
</html>
In this script for example, I used a array to simulate the database, although it works normally.
The variables of session or cookies have lifetime, and can be used on any page, provided they have been set, as I did in the example above. Although the script PHP be unified with the html form nothing changes, even if they were in different files, the message would still be printed without any problem.
Remove the
header()
or error via get– rray