Reputation: 665
I have a problem with cookies.
Basically I'm trying to store the user's session ID as a cookie like so:
setcookie("CheckoutSessionID",session_id(),time()+3600);
This works fine on my site, the cookie has the correct value and is valid for long enough. However, my site redirects to PayPal so the user can confirm a payment. The user is then redirected back to my site. It's when the user is redirected back to my site that ALL cookie variables are gone.
As in, print_r($_COOKIE)
, var_dump($_COOKIE)
etc have no values. This only occurs after being directed to and from PayPal.
Any ideas as to why this is happening?
Thanks in advance for all help, I'm stumped!
Upvotes: 2
Views: 6197
Reputation: 65
I got similar problem cookies being removed after redirect from Paypal.
it took me a while to figure out where was a problem.
Samesite=**"Strict"** // Removes cookies after redirect from Paypal.
Samesite=**"Lax"** // does not remove cookies after redirect from Paypal.
Upvotes: 1
Reputation: 665
Okay I've been digging quite deep and realised that an earlier question of mine is related:
PHP sessions and session_start()
Basically I had problems because PHP sessions were being deleted when I went to PayPal and back. However, I believe this was actually caused by the session COOKIE being destroyed, not the entire session.
I also found this topic here: Do PHP sessions get lost when directing to a payment gateway?
Answer given by someone suggests using a GET request with the return URL to send data back, instead of using cookies or sessions.
The whole reason I was using a cookie in the first place was to save the user's session ID, as the sessions weren't working properly, so basically I've just made my return URL something like this:
mydomain.co.uk/mypage.php?SessionID=[session ID goes here] and then obtained it then set the user's session ID to it.
Sorted! For now... I mean I'll probably end up hitting another brick wall due to cookies/sessions not working properly.
Thanks everyone for your help :)
Upvotes: 3
Reputation: 146191
Actually whatever is happening (cookie is being empty), logically it's right. When you submit a page/make request the browser sends the cookie
from the client's computer with the request so that you can find the cookie
in the cookie
variable.
But once you redirect the user to another external page/site and come back again to your page then you should not get the cookie
in the cookie
variable because (in your case) when the user is getting back to your site from the paypal
the paypal
is not submitting the cookie
with the request.
In this case you can save your data in the database before you redirect the user to the paypal
and once the user comes back to your site you can retrieve that data from the database.
Upvotes: 3