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.

AKor
  • 8,550
  • 27
  • 82
  • 136
  • 2
    did you make sure that was something in the `$foo` variable? – Naftali May 05 '11 at 20:18
  • 7
    Let's see more code. – Joe Phillips May 05 '11 at 20:18
  • Turn on full error reporting. One possible reason is that there might be a problem writing to session files by the server (if you are not using database for session handling) – McKracken May 05 '11 at 20:28
  • @Joe That's all of the relevant code. @Neal Yes, I'm able to output the session variable on the same page that it was set, but not on the page I want it to output on. – AKor May 05 '11 at 20:32
  • 2
    @Sennheiser No, that's all you *think* is relevant. It's likely you're overlooking something. – Joe Phillips May 05 '11 at 20:40
  • 1
    I'd recommend reading http://stackoverflow.com/questions/107683/when-and-why-should-request-be-used-instead-of-get-post-cookie/107737#107737 as well. – Rich Bradshaw May 05 '11 at 21:24
  • If you can set session vars to a string, but not to a variable, it means that your `$_REQUEST` variables are not being set. Are you sending those values correctly? – gen_Eric May 05 '11 at 21:29
  • 2
    If it sets to a string, then SESSIONS AREN'T YOUR PROBLEM! Don't blame the session because you're passing it garbage. – Chris Eberle May 05 '11 at 21:33
  • Yes, I'm outputting those variables elsewhere. I can set the session vars to the variable, but that value will go away if I go back to Page 1. – AKor May 05 '11 at 21:37
  • OK, let me put this another way: if `$_SESSION['test'] = 'hello'` works, but `$_SESSION['test'] = $var` doesn't, then `$var` isn't being properly set. Period. Turn on php warnings to get more useful messages. – Chris Eberle May 05 '11 at 21:44
  • I know the variables are being properly set. I'm outputting those variables all over Page 2. I'm not sure why the session variable will take a string but not a variable. That's why I posted this question. – AKor May 05 '11 at 23:08
  • @Sennheiser The answer is that it should be working fine in normal circumstance. You are in a special circumstance and refuse to provide all the code and therefore will not receive your answer in the 5 seconds it should have taken the first time around. – Joe Phillips May 05 '11 at 23:11
  • I'll just leave this here: http://slash7.com/2006/12/22/vampires/ – Chris Eberle May 06 '11 at 04:21

4 Answers4

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.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • @Sennheiser I'm assuming you are doing this on both Page1 and Page2? – Joe Phillips May 05 '11 at 20:55
  • @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 – AKor May 05 '11 at 20:59
  • 2
    @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 – Joe Phillips May 05 '11 at 21:01
3

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

Ivan
  • 491
  • 6
  • 15
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)
Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
-1

You would also need to register the session using

session_register @ php.net

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
  • 3
    "This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged." – gen_Eric May 05 '11 at 20:29
  • 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 M Grech May 21 '11 at 07:38
  • @Oliver: Call `session_start`, then just do `$_SESSION['key'] = $value`. – gen_Eric May 22 '11 at 19:55