AKor
AKor

Reputation: 8882

PHP $_SESSION variables not storing values

I'm trying to use PHP session variables to carry data over multiple pages. I am using session variables on many parts of my site, but this is the first place where they don't work.

I'm setting it like this:

$_SESSION["savedRetailerName"] = $foo; 

And calling it like this:

echo $_SESSION["savedRetailerName"]; 

The session id remains the same between these two pages, and I'm sure that I'm setting the variables right and that they are being called right. I start the session correctly, and even on that particular page, other session variables are being shown properly.

How can I begin to debug this odd behavior?

Edit:

There are two different pages I'm currently dealing with. Page 2 sets the session variables, and there is a button that will return the user to Page 1. The idea is to still have the fields in Page 1 filled in if the user wishes to return to Page 1.

It is not a cache problem, and I can return other session variables in the exact same spot in my code as where I am failing to return these variables.

The only other code that may be pertinent is the back button handler (jQuery):

  $('#backButton').live('click',function() {
        window.location.replace("page 1");
    });

Edit 2:

I believe this isn't working because of something with variables here:

    <?php
    $retailerName = $_REQUEST["retailerName"];
    $description = $_REQUEST["description"];
    $savingsDetails = $_REQUEST["savingsDetails"];
    $terms = $_REQUEST["terms"];
    $phone = $_REQUEST["phone"];
    $address = $_REQUEST["address"];
    $zone = $_REQUEST["zone"];
    $dateExp = $_REQUEST["dateExp"];
    $tag = $_REQUEST["tag"];

    $_SESSION["rn"] = $retailerName;
    $_SESSION["de"] = $description;
    $_SESSION["sd"] = $savingsDetails;
    $_SESSION["tm"] = $terms;
    $_SESSION["ph"] = $phone;
    $_SESSION["ad"] = $address;
    $_SESSION["zo"] = $zone;
    $_SESSION["ex"] = $dateExp;
    $_SESSION["tg"] = $tag;
    ?>

I am able to set any session variable to a string, but it won't set to a variable.

Upvotes: 0

Views: 1313

Answers (4)

Joe Phillips
Joe Phillips

Reputation: 51120

  1. You have spaces before your <php tag
  2. You don't have session_start() anywhere
  3. You are using the $_REQUEST variable which is sketchy (use $_GET or $_POST instead)

Upvotes: 1

Ivan
Ivan

Reputation: 491

remove all non printable characters before <?php you may not see them..

Upvotes: 3

Oliver M Grech
Oliver M Grech

Reputation: 3171

You would also need to register the session using

session_register @ php.net

Upvotes: -1

Chris Eberle
Chris Eberle

Reputation: 48775

You want to use session_start before you set or use any session variables. You only need to call it once.

If it's working in other places, odds are that this particular block of code is being executed before session_start is called.

Upvotes: 6

Related Questions