Link to home
Start Free TrialLog in
Avatar of maeve100
maeve100

asked on

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?
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Did you use session_start in all the scripts?
Avatar of maeve100
maeve100

ASKER

Yup. Its there on all the pages.
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.
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.
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>";
Are all of your scripts in the same domain?  Session variables don't cross domain boundaries.
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

What is the connection between 2.php and 3.php ... how does 3.php get started?
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.
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.
Avatar of Deja Anbu
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....
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.
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.
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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial