PHP Post Problem

Hi there,

I submit a form on 1.php to 2.php.  On 2.php I use the following code:
$_SESSION['myPurchased'] =  $_POST['checkout'];
echo "session purchased: " . $_SESSION['myPurchased'];

And this prints the correct value of the Post Checkout to the screen.  The variable is not touched further down on the page.  

Then in 3.php I want to print the myPurchased session using:
echo "session purchased: " . $_SESSION['myPurchased'];
- but the session value is not carried over.  Nothing but "session purchased:" is printed to the screen.  If I hard code in a value into the 2.php code (ie. $_SESSION['myPurchased'] =  1234;) then the 1234 value prints on 3.php.  It seems to have something to do with the variable being set up with a POST value.  

Using REQUEST has the same outcome, and GET does not work.  Its on an IIS server, and uses PHP5.  

Any ideas to get my session variable to truly be a session, so I can use it later on in my code?
maeve100Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Cornelia YoderConnect With a Mentor ArtistCommented:
DId you go to that thread I just posted ... there are several server configuration things to check.
0
 
Cornelia YoderArtistCommented:
Did you use session_start in all the scripts?
0
 
maeve100Author Commented:
Yup. Its there on all the pages.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
maeve100Author Commented:
I have even tried using implode(); to attach something to the Post Checkout text.  On 2.php it prints correctly, but on 3.php just the text I imploded onto the post checkout prints.
0
 
JayDiabloCommented:
Could you maybe cut and paste some of the relevant lines in each of the PHP files (2 and 3 I suppose are the only important ones).

Does $_SESSION have anything in it on 3.php? (print_r $_SESSION);

Also, just as a sanity check, could you make sure that the output of the following is the same on each page?

echo session_id();

Open in new window


http://php.net/manual/en/function.session-id.php

That might help debug this issue.  What you describe in your question should work, and I don't see any glaring errors in your method.
0
 
maeve100Author Commented:
Good thing to check, the session_id is the same for all the pages. :-)
Unfortunately, the code really is as simple as what I put up previously.  The way POST variables function on this server seems to be kind of wonky though.  I have had to change a lot of the format because its an IIS server.  I stick the code at the top, since the rest of the code on the page has nothing to do with these variables.  I am just trying to carry it through to the end.

2.php
session_start();
$_SESSION['myPurchased'] = $_REQUEST['checkout'];
echo "session purchased: " . $_SESSION['myPurchased'];

3.php:
session_start();
echo "<p style='color:#ffffff'> session purchased: " . $_SESSION['myPurchased'];
echo "<br/>";
echo session_id();
echo "<br/>";
echo "</p>";
0
 
Cornelia YoderArtistCommented:
Are all of your scripts in the same domain?  Session variables don't cross domain boundaries.
0
 
maeve100Author Commented:
Yes, they are all on the same domain, same folder, too.

Also, I did a var dump and it says that the session is there, but on 3.php it turns into:
["myPurchased"]=> NULL
0
 
Cornelia YoderArtistCommented:

What is the connection between 2.php and 3.php ... how does 3.php get started?
0
 
maeve100Author Commented:
3.php starts with
<?php
session_start();
echo "<p style='color:#ffffff'> session purchased: " . $_SESSION['myPurchased'];
echo "<br/>";
echo session_id();
echo "<br/>";
echo "</p>";
....

2.php simply does a
<script type='text/javascript'> window.location = '3.php'</script>
To forward the process along to 3.php.
0
 
Cornelia YoderArtistCommented:
ok, try this:

in 2.php, replace the window.location with an include of 3.php and then see if the echoes show the variables correctly.  This will tell us if the problem is in the javascript transfer of control.
0
 
dejaanbuCommented:
You can try posting the value to 3.php  instead of redirecting with javascript

In 2.php
<?php
session_start();
echo "<p style='color:#ffffff'> session purchased: " . $_SESSION['myPurchased'];
echo "<br/>";
echo session_id();
echo "<br/>";
echo "</p>";
?>
<form action="3.php" method="post">
<input id="pur_id" name="pur_id" value="<?php echo $_SESSION['myPurchased'];?>"/>
//SOME BUTTON CODE
</form>

Open in new window


try with this and let us know....
0
 
maeve100Author Commented:
The session works when I include the 3.php inside of 2.php - so its not the redirect I am guessing.

I can try posting the variable, but the problem is that I need this session to carry through a bunch of other pages, I can't post the data each time, it would just make a huge mess.

Also, here is the kicker, I just realized that in IE and Chrome the code functions properly!  So in FireFox something is forcing that session to go Null.
0
 
Cornelia YoderArtistCommented:
Bad news, I had a situation some time ago that was similar, but it was just one of several variables that wouldn't work in FF, the others all came through normally.   So try this .... add a second SESSION variable and see if either of them works.
0
 
JayDiabloCommented:
Seems that it must be the javascript redirect then (if different browsers work and one doesn't).  Perhaps you should try and construct a full URL to redirect to rather than just redirecting to "3.php".  Perhaps firefox thinks it's a different domain or something, so doesn't pass along the right cookie id (seems odd that the session_id would be the same though).

As another possible way to debug, do you have access to the directory where PHP stores the session files?  It might be interesting to see if the file for your session changes at all between the 2nd and 3rd pages (if that value is actually being removed, or if it's just not being given to the script).

If you re-load 2.php after being re-directed to 3.php is your session data available?
0
 
maeve100Author Commented:
Second Session variable failed :(.

I tried putting the code on a Linux box, and it worked just fine in all browsers.  It seems to be related to the IIS server that it has to be hosted on.  What an annoying error!  And on *some* machines Firefox will work (exact same version.)  This is insane.  I have to figure out a work around.
0
 
maeve100Author Commented:
I am going through the post now, lots of things to check.

One interesting thing that came up is that when I do a varp_dump on page 2.php (where the session is holding the correct value) I get:
["myPurchased"]=> string(8) "cart1234"

But when we move to 3.php that above myPurchased var_dump entry disapears, but this shows up instead:
["cart1234"]=> string(8) "cart1234"

When I try this on a browser where the session passes correctly, IE, both
["myPurchased"]=> string(8) "cart1234"
and
["cart1234"]=> string(8) "cart1234"
appear on page 3.php.

Any idea what where this session comes from?  The one that has the same name as its contents?
0
 
JayDiabloConnect With a Mentor Commented:
Is the code concise enough that you can quickly find all places where you're setting anything on $_SESSION?

Also check for calls to the function session_register().
0
All Courses

From novice to tech pro — start learning today.