Message to user after header change

1

I have a small problem with PHP, I need to redirect the user, but I wanted to notify you of what happened, I redirect it with header() , but if I try to send something before header fails, Any tips on how I can send a message to usuario and modify header ? I tried to change the header before sending the message, but in this case the message was not sent.

Here is the code I made, I'm using MySql and wanted to notify the user when it was registered and when there was a failure:

<?php
    error_reporting(E_ALL);ini_set('display_errors', 1);
    $host = "localhost:8889";
    $user = "root";
    $pass = "root";
    $banco = "ArrayEnterprises";
    $conexao = mysqli_connect($host, $user, $pass, $banco);
    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $senha = md5($_POST["senha"]);
    $img = $_POST["img"];
    $inserir = "INSERT INTO Usuario (nome, email, senha, foto) VALUES('{$nome}', '{$email}', '{$senha}', '{$img}')";
     if($query = mysqli_query($conexao, $inserir)){
        if(mysqli_affected_rows($conexao) > 0){
            header("location: index.html");
            echo"<script language='javascript' type='text/javascript'>alert('Cadastrou!');</script>";
        }
    } else {
        echo"<script language='javascript' type='text/javascript'>alert('houve um erro');</script>";
        header("location: cadastro.html");
    }
    mysqli_close($conexao);
?>
    
asked by anonymous 03.12.2015 / 05:01

2 answers

2

A first option would be to consider using sessions by storing the message on the source page, and capture that message on the same page, or on another page, after recirect.

<?php
# pagina principal
session_start();

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    $_SESSION['msg'] = 'Redireccionado com sucesso';
    header("Location: redirect.php");
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    $_SESSION['msg'] = 'Erro';
    header("Location: redirect.php");
} else {
    print "</p>Clique num dos links abaixo</p>";
}
#capturar mensagem
if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
    print "<script>alert(\"{$_SESSION['msg']}\")</script>";
}

?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>

You could also use the javascript itself to redirect after displaying the alert by doing the following:

<?php

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    print "<script>alert('sucesso');</script>";
    print "<script>location.href='redirect1.php';</script>";
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    print "<script>alert('erro');</script>";
    print "<script>location.href='redirect1.php';</script>";
} else {
    print "</p>Clique num dos links abaixo</p>";
}


?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>

In a third option, you could still print the message in that same script, and then do the redirect:

<?php

if(isset($_GET['redirect']) && !is_numeric($_GET['redirect'])){
    print "sucesso";
    print "<script>setTimeout(\"location.href='redirect1.php'\", 1000);</script>";
} elseif(isset($_GET['redirect']) && is_numeric($_GET['redirect'])) {
    print "erro";
    print "<script>setTimeout(\"location.href='redirect1.php'\", 1000);</script>";
} else {
    print "</p>Clique num dos links abaixo</p>";
}


?>
<a href="?redirect=1">ver erro</a> | <a href="?redirect=ir">ver mensagem</a>
    
03.12.2015 / 05:58
0

As the friend said earlier use:

$_SESSION['mensagem'] = 'mensagem';

Then do your header, and in the redirected page show the session:

echo $_SESSION['mensagem']
    
04.12.2015 / 10:43