Sessions in PHP

2

I'm working on a system that has a 'search filter' on the home screen. For that, in my header I put a select / combo box and an 'OK' button.

To handle this I decided to use sessions, where I can save the value that the guy chose and use on all the necessary pages. But this session is having a problem.

- Every time he goes to some other page, the session seems to be zero and I do not have the information anymore:

    session_start();        
    $geral = $_GET['slcGeral'];
    $_SESSION['Geral'] = $geral;

- To test I passed the direct value and so it works normal:

    session_start();        
    $geral = 1;
    $_SESSION['Geral'] = $geral;

What can it be?

    
asked by anonymous 19.09.2014 / 05:20

1 answer

11

If you always set the value of $geral equal to $_GET['slcGeral'] when the GET is empty it will clear the session ... You must first check if the GET is set, then set the session:

session_start();        
if (isset($_GET['slcGeral'])) {
    $geral = $_GET['slcGeral'];
    $_SESSION['Geral'] = $geral;
} else {
    $geral = $_SESSION['Geral'];
}

I also suggest standardizing the variable name to avoid confusion:

session_start();        
if (isset($_GET['geral'])) {
    $geral = $_GET['geral'];
    $_SESSION['geral'] = $geral;
} else {
    $geral = $_SESSION['geral'];
}

OBs: This implies also modifying the name of the field in the form or URLs of the links.

    
19.09.2014 / 12:26