Look, a recommendation. To set error messages, use sessions or even cookies . In addition to being feasible for this task, they are easy to manipulate 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 an array to simulate the database, although it works normally.
Session or cookies variables have a lifetime, and can be used on any page as long as they have been defined, as I did in the example above. Although the PHP script is unified with the HTML format, nothing changes, even if they were in different files, the message would continue to print without any problems.