unset () being executed within a wrong conditional

0

I have an interesting problem: my unset ($ _ SESSION ['x']) is running inside an IF that is not true. Look at my code:

<?php
// Codigo do estado
$cod_estado = $_GET['s'];

// Função da classe Usuario 
$cont_resultado = count(Usuario::CS_GetUsuarios("WHERE cod_estado = " . checarSql($cod_estado)));

// Já tentei < 1, <= 0
if ($cont_resultado == 0) {
    echo "<script>alert('cheguei aqui');</script>"; // So para exemplo
    unset($_SESSION['usuario_estado']);
}
?>

Even if $cont_resultado == 20 it executes unset() do not know why.

Function CS_GetUsuarios() :

public function CS_GetUsuarios($sql_aux = false)
{
    global $obj_conexao;
    $str_sql = "SELECT * FROM usuario $sql_aux";

    $vet_dados = $obj_conexao->query($str_sql)->fetchAll();
    return $vet_dados;
}

Let's suppose I run the following query: SELECT * FROM usuario WHERE cod_estado = 8 and this result returns me in count a value of 20 (a false conditional). It will not run <script>alert</script> but will run unset() .

Giving a var_dump() before the IF the session exists .

  • If the IF is true (count == 0) the session will not exist at the end of the script.
  • If the IF is false, it will exist at the end of the script.

The whole problem is when I move to some other page of my site, the session is destroyed (even if you have not entered the IF).

The strange thing is that I commented on the unset($_SESSION['usuario_estado']); line and that's it, the problems are over. The session still exists even after I move on to other pages.

Complementing: on line 1 of index.php I make an include include_once "includes/include.php"; that contains the main functions of my script.

File include.php :

<?php
//error_reporting(E_ALL);
//ini_set( 'display_errors','1');
//ob_start('ob_gzhandler');

// Header
session_start();

// Daqui para baixo só funções
?>

include.php is present on ALL pages of the script because they are all included through index.php .

What's wrong?

UPDATE: Just for the purpose of information, I was able to solve the problem. My checarSql() function was passing $cod_estado as a string to the query, and should be an integer. Using Usuario::CS_GetUsuarios("WHERE codEstado = $cod_estado") was successful.

    
asked by anonymous 22.05.2017 / 15:37

0 answers