Jump to content

PHP $_SESSION within PW


a-ok
 Share

Recommended Posts

I need to add some saved $_POST data into a $_SESSION. This works fine on a page outside of PW (templates) but isn't returned on any page of PW (within a template or head.inc). I believe this is because $_SESSION is already started. Is there a way to push my $_POST data into the $_SESSION?

$_SESSION['formData'] = $_POST['formData'];

On a page within PW it is returned as NULL.

Link to comment
Share on other sites

I am not sure how you are getting from the page that sets the $_SESSION into PW. Are you using a header redirect from one to the other?

Maybe you could bootstrap PW in the non PW page that collects the $_POST data. Then you could make use of $session in that non PW page.

Maybe you could use a cookie instead?

Link to comment
Share on other sites

13 minutes ago, adrian said:

I am not sure how you are getting from the page that sets the $_SESSION into PW. Are you using a header redirect from one to the other?

Maybe you could bootstrap PW in the non PW page that collects the $_POST data. Then you could make use of $session in that non PW page.

Maybe you could use a cookie instead?

Thanks for the help, Adrian. I'm really struggling to work this out.

Basically I am using the PayPal PHP SDK and upon submission, using $.ajax I am passing the data (which is localStorage) to a page payment__paypal.php (which sits in the directory /api/ alongside /site/ and /wire/. From here I declare the PayPal API stuff and set my $_SESSION['formData'] = $_POST['formData']; and then on redirects to the homepage from PayPal (once successful) where I have <?php require('../../api/redirect__paypal.php'); ?> included in my head.inc. In this file it takes the $_SESSION['formData'] and does a few things with it, or at least it should. If I redirect from PayPal directly to this file (rather than the homepage with the file required) it works fine but for some reason it's not storing the session when I use the require on the homepage (head.inc).

If I add $_SESSION['test'] = 'hello_world'; in the redirect__paypal.php file it seems to add it to the $_SESSION if I var_dump it on the homepage so I'm unsure why the $_SESSION['formData'] isn't being saved across?

I realise this is perhaps more a PHP issue than a PW one but it's weird it works outside of the non PW page.

Link to comment
Share on other sites

It sounds to me like things are being lost in the redirect to Paypal and back. Paypal should return most things back to you so you can grab them again. What I do is create and populate a PW page with the user's details, submit them to Paypal for payment, and then on return I can look up the user by an ID that I assign to them (stored in their page and also sent to Paypal). With that I can update their page with the payment details (verification of payment, etc).

This of course does result in some entries which weren't finalized - sometimes people choose to back out at the last minute, but at least you have more info on this. 

Also, getting OT here, but are you making use of Paypal's IPN system (https://github.com/paypal/ipn-code-samples/blob/master/php/PaypalIPN.php) to verify payment?

  • Like 2
Link to comment
Share on other sites

Thanks, Adrian.

I managed to bootstrap in PW and change it all to $session and it worked! Must be that it doesn't like the multiple variables... although it does say this on the $session API page https://processwire.com/api/variables/session/

Quote

You may also use PHP's $_SESSION superglobal variable in ProcessWire, but note that $session uses a different namespace within $_SESSION, so the two can't be used interchangeably for the same variables... it's best to stick to using one or the other, and we recommend using $session.

I hate not knowing why it wasn't working but I had tried and tested everything until using $session worked so perhaps that was just it.

Re PayPal... not I'm not... I'm using something similar to this: https://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html and https://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/ExecutePayment.html and relying on the Approval URL. What do you think?

Thanks again for all your help.

  • Like 1
Link to comment
Share on other sites

I also had some troubles with sessions and form data sometimes ago.

In my case I wanted to store multiple infos (fe first name, last name, email,...) inside a session.  I have tried it with an array, but this didnt work. So I ended up to put my form data into an array and convert this array into a json array before storing it into a session and this works. After that I have outputed the form data by converting back the json array to a standard array (so jsonencode, jsondecode) was the key of success for me to work with multiple data and sessions.

Best regards

 

  • Like 1
Link to comment
Share on other sites

20 hours ago, oma said:

I hate not knowing why it wasn't working but I had tried and tested everything until using $session worked so perhaps that was just it.

Re PayPal... not I'm not... I'm using something similar to this: https://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html and https://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/ExecutePayment.html and relying on the Approval URL. What do you think?

Glad you got it working that way.

As for the approach you are using - honestly not sure - I haven't worked with PayPal for a few years, so I am probably out of date on the latest practices, but at the time IPN was the way to go, but maybe now there is a better approach to verifying a payment as being legit and not a spoofed POST to your form.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...