Solved

PHP Post Problem

Posted on 2011-02-15
19
807 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:maeve100
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 34902903
Did you use session_start in all the scripts?
0
 

Author Comment

by:maeve100
ID: 34902927
Yup. Its there on all the pages.
0
 

Author Comment

by:maeve100
ID: 34902985
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
 
LVL 4

Expert Comment

by:JayDiablo
ID: 34903036
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
 

Author Comment

by:maeve100
ID: 34903077
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
 
LVL 27

Expert Comment

by:yodercm
ID: 34903093
Are all of your scripts in the same domain?  Session variables don't cross domain boundaries.
0
 

Author Comment

by:maeve100
ID: 34903105
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
 
LVL 27

Expert Comment

by:yodercm
ID: 34903148

What is the connection between 2.php and 3.php ... how does 3.php get started?
0
 

Author Comment

by:maeve100
ID: 34903175
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:yodercm
ID: 34903190
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
 
LVL 14

Expert Comment

by:dejaanbu
ID: 34905242
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
 

Author Comment

by:maeve100
ID: 34910173
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
 
LVL 27

Expert Comment

by:yodercm
ID: 34910205
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
 
LVL 4

Expert Comment

by:JayDiablo
ID: 34910313
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
 
LVL 27

Expert Comment

by:yodercm
ID: 34910316
0
 

Author Comment

by:maeve100
ID: 34910648
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
 
LVL 27

Accepted Solution

by:
yodercm earned 250 total points
ID: 34911017
DId you go to that thread I just posted ... there are several server configuration things to check.
0
 

Author Comment

by:maeve100
ID: 34912035
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
 
LVL 4

Assisted Solution

by:JayDiablo
JayDiablo earned 250 total points
ID: 34912273
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now