unset() being executed within a wrong parole

Asked

Viewed 37 times

0

I have an interesting problem: my unset($_SESSION['x']) is running inside an IF that is not true. See 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 he executes the unset() I don’t 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;
}

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 will not execute the <script>alert</script> but will execute the unset().

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

  • If IF is true (Count == 0) the session will not exist at the end of the script.
  • If 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 I have not entered the IF).

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

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

Filing cabinet 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
?>

You see, the include.php is present on ALL pages of the script as they are all included through the index php..

What’s wrong with it?

UPDATE: For information purposes only, I was able to solve the problem. My function checarSql() was passing the $cod_estado as string à query, it should be an integer. Thus using Usuario::CS_GetUsuarios("WHERE codEstado = $cod_estado") succeeded.

  • Are you removing the content elsewhere or at this time it does not exist.

  • Before if, check if the index exists with isset()

  • I’ve already given one var_dump($_SESSION) before the IF and the $_SESSION['usuario_estado'] is there, it’s true.

  • Exactly. Possibly you have created a false truth by thinking it is this unset which is being executed. It is not, but the alert would also be. Or you called the unset in another part of the code, as Rayann said, either you are checking the existence of the session in the wrong way, thinking it doesn’t exist, or even creating the session in the wrong way. But almost certainly not this unset the problem.

  • And if it happens after the if it ceases to exist?

  • Anderson, I took the test here, after the IF it ceases to exist, in case you get paroled up on the count is < 0.

  • Believe me, there is no other place where some other is being executed unset(). I’m getting frustrated with this.

  • I edited it with some new information, maybe it’ll help.

  • This information that the session continues to exist after the if, but does not exist on another page shows that the problem is not in the if. Are you properly utilizing the session_start on this other page? If possible, let us know what is the code of this.

  • Anderson, I added the contents of the file where Session starts.

  • Anderson, I added an update where I was able to fix the problem. Thanks to everyone who helped.

Show 7 more comments
No answers

Browser other questions tagged

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