0

I am trying to store a value obtained from a URL variable into a SESSION variable.

Here is the URL:

<a href="webpage.php?store=<?php echo 'Ace'; ?>">Ace Hardware</a>

Here is the SESSION coding, which retrieves the variable, but loses the variable value upon leaving the page.

$_SESSION["store"] = $_GET["store"];
$shopByStore = $_SESSION["store"];

If I plug in the value in quotes as it is below (see "Ace" in code below), it works. But it doesn't work in the code above using the GET method ($_GET["store"])

$_SESSION["store"] = "Ace";
$shopByStore = $_SESSION["store"];
1
  • Did you started the session ? Commented Mar 31, 2014 at 23:16

2 Answers 2

2

The problem is that you override the $_SESSION["store"] with the $_GET["store"] each time whether the get request exists or not, so it basically only uses $_GET["store"]. You could use this instead:

if (isset($_GET["store"])) {
    $_SESSION["store"] = $_GET["store"];
}
Sign up to request clarification or add additional context in comments.

Comments

2

At first you need to start the session and then make sure it's already not stored in the session (if it's passed in the $_GET) and if not already saved in the session then store it:

// The first line in your script
session_start();

if (isset($_GET["store"])) {
    if (!isset($_SESSION["store"])) {
        $_SESSION["store"] = $_GET["store"];
    }
}

Read more on PHP manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.