0

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.

13
  • 2
    did you make sure that was something in the $foo variable? Commented May 5, 2011 at 20:18
  • 7
    Let's see more code. Commented May 5, 2011 at 20:18
  • 2
    @Sennheiser No, that's all you think is relevant. It's likely you're overlooking something. Commented May 5, 2011 at 20:40
  • 1
    I'd recommend reading stackoverflow.com/questions/107683/… as well. Commented May 5, 2011 at 21:24
  • 2
    If it sets to a string, then SESSIONS AREN'T YOUR PROBLEM! Don't blame the session because you're passing it garbage. Commented May 5, 2011 at 21:33

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Sennheiser I'm assuming you are doing this on both Page1 and Page2?
@Joe: Yes. As I stated before, I have other session variables that output perfectly in the same location in my code as the session variables I am trying to carry over
@Sennheiser You don't think that your other code that is modifying/using the session variable is relevant to your problem? Please, just show the code
3

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

Comments

1
  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)

Comments

-1

You would also need to register the session using

session_register @ php.net

3 Comments

"This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged."
Much apologies, somethimes it's hard to keep up with all the change logs. So with that said, what's the recommended way of "registering" a $_SESSION? Just assign it a value and that's it or what? Thanks for the tip off
@Oliver: Call session_start, then just do $_SESSION['key'] = $value.

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.