I am sending an Ajax request to a PHP script where I am using setcookie
to set the cookies on an external domain. After that, I am sending another Ajax request to the external domain to read the cookies. I am unable to achieve the results. I am using jQuery to send Ajax requests.
The PHP script:
<?php
// yummy cookies ?
header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
header("Access-Control-Allow-Credentials: true");
if($_GET['read'] == true) {
echo json_encode(
array(
'user_id' => $_COOKIE['user_id'],
'twitter_name' => $_COOKIE['twitter_name'],
'facebook_id' => $_COOKIE['facebook_id']
)
);
}
if($_GET['write'] == true) {
if($_GET['type'] == 'twitter') {
setcookie('twitter_name', $_GET['twitter_name'], time() + 1209600);
setcookie('user_id', $_GET['user_id'], time() + 1209600);
}
if($_GET['type'] == 'facebook') {
setcookie('facebook_id', $_GET['facebook_id'], time() + 1209600);
setcookie('user_id', $_GET['user_id'], time() + 1209600);
}
}