仅在GET请求具有值时才更改会话变量

I am looking to set a session variable based on a search conducted by the user. The idea is that the search is populated with their last search wherever they go on the site.

I have the following code that I thought would set the variable if the variable geo-box was present and use the saved variable if it isn't, but this doesn't work...

    session_start();

if(isset($_GET['geo-box'])){
    echo $_SESSION['town'] = $_GET['geo-box'];
} else {
    echo $_SESSION['town'];
}
session_start();

if(isset($_GET['geo-box']))
  $_SESSION['town'] = $_GET['geo-box'];

echo $_SESSION['town'];

You can't echo a variable while defining it.

Best of Luck!

You are trying to echo a variable and set it in the same line.

Try this:

session_start();

if( isset($_GET['geo-box']) ) {
    $_SESSION['town'] = $_GET['geo-box'];
}

echo $_SESSION['town'];

You can not echo a value and assign it at the same time. Give this a try!

Hope this helps.