Solved

PHP Post Problem

Posted on 2011-02-15
19
814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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:Cornelia Yoder
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:Cornelia Yoder
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
 
LVL 27

Expert Comment

by:Cornelia Yoder
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:Cornelia Yoder
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
 

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:
Cornelia Yoder 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

705 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