0

I need to execute a javascript code into which I have to set some cookie info, redirect to a php page and get such info by php code. I must not use such info as url parameters to transfer to php.

I tried this in javascript:

currentClass = { 'date': '..some date..', 'time': '..some time..', 'instructor': '..some instructor..' } var result = JSON.stringify(currentClass); var date = new Date(); date.setTime(date.getTime()+3600); document.cookie = "BOOKING_CLASS=" + result + "; expires=" + date.toGMTString() + ";"; window.location.href = '/apps/mindbody/add/bookingmember';

location /apps/mindbody/add/bookingmember is routing by .htaccess apache file to a specific php file

The PHP contains:

if (!isset($_COOKIE['BOOKING_CLASS'])) { show_error(); } 

The only data that $_COOKIE has is PHPSESSIONID so such required cookie is not present and error is shown.

I tried setting the path and domain without success!

document.cookie = "BOOKING_CLASS=" + result + "; expires=" + date.toGMTString() + "; path=/apps/mindbody/add/bookingmember; domain=mydomain.com"; 

when running, browser stop on breakpoint to show cookies value:

enter image description here

Following is the browser cookie value:

enter image description here

No set cookie value exists in PHP.

enter image description here

Thanks for any help

1 Answer 1

1

The problem seems to be with expiry time of the cookie. Your cookie gets expired before you try to read in PHP. Try adding an hour to your cookie expiry time. For e.g:

var date = new Date(); date.setHours( date.getHours() + 2 ); document.cookie = "BOOKING_CLASS=" + result + "; expires=" + date.toGMTString() + ";"; 

And also I assume that you are in the same domain while reading the cookie.

Sign up to request clarification or add additional context in comments.

3 Comments

it didn't work, $_COOKIE['BOOKING_CLASS'] shows <uninitialized>
Are you able to share some more info? Do you see the cookie being set in your browser console? It works perfectly fine after modifying the expiry time at my end with an "htaccess" redirect as you mentioned.
I've found the cause of the problem, the core main platform, is clearing cookies at start site, so is not a php problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.