I have a strange problem- In my website https://www.example.com I have this JS code, triggered on some JS event:
$.post( 'https://www.example2.com/api/save_cookie.php',
{ storageData: storageData },
function(data) {console.log(data)}
);
In https://www.exmaple2.com, the file save_cookie.php looks like this:
$test = setcookie('test_cookie', '1', time() + (10 * 365 * 24 * 60 * 60), '/');
echo $test;
When I trigger the JS event, I can see in the network tab that everything is okay, that the request sent successfully to the url https://www.exmaple2.com/api/save_cookie.php, and I can see "1" printed in the console, from the callback function.
But in https://www.exmaple2.com, $_COOKIE['test_cookie']
, is null
.
However, If I just browse the url www.exmaple2.com/api/save_cookie.php, and check the variable $_COOKIE['test_cookie']
after that, I can see that the value is '1', as expected.
Any ideas why the cookie is not set when posting the request with ajax?
Thanks!