Sessions in PHP

Asked

Viewed 161 times

2

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

To treat this I decided to use Sesssions, where I can store the value that the guy chose and use on all the necessary pages. It’s just that this Miss is having a problem.

-Every time he goes to some other page, Session looks like Zera and I don’t have the information anymore:

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

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

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

What can it be?

  • 2

    Where are those codes you put in the question? You have no idea how you are using either the session or keeping the values the way you have expressed them. Try [Dit] the question so that it becomes self-sufficient with the details provided, to increase the chance of having an answer that solves your problem. Suggested reading: [Ask]

  • 1

    First check if a get this set, only then set the Session.

  • an isset would settle quietly

1 answer

11


If you always set the value of $geral equal to $_GET['slcGeral'] when the GET comes empty 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'];
}

Note: This also implies modifying the field name in the form or links Urls.

Browser other questions tagged

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