User message with time and page forwarding killing session

Asked

Viewed 39 times

1

I am trying to make a redirect after giving a message to the user, but the message is not being displayed and let alone the session being destroyed, I do not know exactly what is wrong, the code I have so far is this:


// SEGURANÇA DE ACESSO 
$sqlUsuario = "SELECT * FROM cadUsuario WHERE IdUsuario = ? AND IdPessoa = ?";
$stm = $conexao->prepare($sqlUsuario);
// DEFINE O TIPO DA VARIÁVEL INT OU STR
$stm->bindValue(1, $IdUsuario, PDO::PARAM_INT); 
$stm->bindValue(2, $IdPessoa, PDO::PARAM_INT);  
$stm->execute();    
$sqlUsuario = $stm->fetchAll(PDO::FETCH_OBJ); 
// CONTAGEM DE REGISTROS RETORNADOS
$contUsuarios = count($sqlUsuario); 
// FECHANDO A CONSULTA
$stm->closeCursor(); 

//SE EXISTIR USUÁRIO MOSTRA CONTEÚDO
if ($contUsuarios > 0) {

// CÓDIGO DA PÁGINA

} else {

// SE NÃO EXISTIR USUÁRIO EXIBIR MENSAGEM

    echo "Erro e redireciona ";    
    sleep(10);  

    // DESTRUINDO A SESSÃO
    session_start();
    session_unset();
    session_destroy();
    session_write_close();
    setcookie(session_name(),'',0,'/');
    session_regenerate_id(true); 
    header("Location:index.php");

}

The message is stylized that way:

echo "<div align='center' class='alert alert-danger'>Erro e redireciona </div>";

  • where this $contUsuarios comes from ?

  • Hello @VME, I entered the code in the question.

  • 1

    Try changing $countsUsuarios = Count($sqlUsuario); to $contUsuarios = $stm->rowCount(); and see if something turns up. If not, give an echo $contUsuarios to see which way out

  • If there are errors, it is not in this question code.

  • To fetchAll can return false. And if you make one count(false) the result will be 1. And in php 5 does not generate Warning. So, best would be to use the @VME suggestion

No answers

Browser other questions tagged

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