$_SESSION Variables Disappear

I’m not sure if this is a php, html, or PayPal question so if I’m in the wrong place, I apologize!

I’m creating some simple, basic ecommerce on our website. The first is a page for membership applications and renewals. The user fills out a form, clicks “submit” and is taken to a “process” page which opens a SESSION and assigns all of the $_POST variables to $_SESSION variables. The user reviews the data they submitted then clicks on PayPal’s “Buy Now” button, goes to PayPal and pays. PayPal “Returns” the user to our return page that says “thanks, blah, blah…” and the php sends an email to the membership chairman with the membership application data. This works great. I use SESSION so that all of the form data the user submitted is still there when PayPal returns them to our site. Next is the problem page. . .

The second page is an event registration page (independent of the membership page.) Following the same concepts above, the user fills out their form, clicks on “submit” and goes to a “process” page that does the SESSION variable assignment thing. This page has 4 items the user can buy (starting with the event registration fee) and uses PayPal’s basic shopping cart via “add to cart” buttons The user then clicks one of the 4 “add to cart” buttons, goes to PayPal, selects quantity, then clicks “continue shopping” (if that’s what they want to do.) PayPal returns them to our “process” page (rather than a “return” page) so that they can select another item, but all of the session variables are now empty! So, is SESSION not the way to do this? Or is there something else I need to do to make the SESSION variables stick? Or should I do this entirely differently? The idea is they return to the process page to add more items to their cart before checking out and paying on PayPal.

Thanks!

It’s hard to say - In theory everything sounds correct. But It depends on how your PHP forms are set up…

Do you have something that calls “session_start” before anything else is executed on that page?

http://us3.php.net/session_start

Yes - The first line of code on the “process” page receiving the FORM data is “session_start()” as follows:

[php]<?php // process_rallyreg.php
/* The following assigns the form data to session variables for later processing */
session_start();
$_SESSION[‘rider_first_name’] = $_POST[‘rider_first_name’];
$_SESSION[‘rider_last_name’] = $_POST[‘rider_last_name’];
$_SESSION[‘corider_first_name’] = $_POST[‘corider_first_name’];
$_SESSION[‘corider_last_name’] = $_POST[‘corider_last_name’];
.
.
.
?>[/php]

As I write this, I think I discovered the problem, but need to know how to fix it. After PayPal closes the shopping cart page and sends the user back to our process page, the process page reloads. However, the $_POST variables no longer exist at this point so the page has no values to use to reload the $_SESSION variables.

What I think I need is some kind of test to see if $_POST exists before loading the $_SESSION variables. How best do I do that?

OK, that was was problem. I added the "if (isset()) test below and everything works.

[php]<?php // process_rallyreg.php
/* The following assigns the form data to session variables for later processing */
session_start();
if (isset($_POST[‘rider_first_name’])) {
$_SESSION[‘rider_first_name’] = $_POST[‘rider_first_name’];
$_SESSION[‘rider_last_name’] = $_POST[‘rider_last_name’];
.
.
.
}
?>[/php]
Thanks Topcoder for asking a simple question that got my mind thinking through the process in the right direction!

That’s usually how it works, everyone just needs a little push.

Sponsor our Newsletter | Privacy Policy | Terms of Service