# Other Languages > jQuery >  [RESOLVED] Setting cookie on remote host

## Datacide

I'm trying to connect two sites together, one that uses JavaScript and one that uses PHP. I want to pass a JavaScript variable to the PHP site, and figured the best way to do this would be to use jQuery.

I created a simple PHP page that creates a cookie with it's POST data. One the JavaScript side, I've inserted this code:



```
$.post('http://remotedomain/setcookie.php', {ProfileID: ProfileID}, function(){
  //successful ajax request
}).error(function(){
  alert('Error setting cookie');
});
```

I keep getting the error alert and I have no idea how to debug it. I tried:


```
.error(function(jqXHR, textStatus, errorThrown) {
  alert(textStatus + " | | + errorThrown);
});
```

But all that returns is "ERROR". Any thoughts/suggestions?

----------


## techgnome

I'm not sure you can do it this way... cookies have to be set BEFORE any output is sent to the client... so by the time your jQuery runs, it's too late... The reason for this is because cookie information is sent as part of the header information when the page is sent down to the client. 

-tg

----------


## Datacide

The cookie is set in the remote PHP file:



```
<?php
if ($_POST['ProfileID']) {
  $ProfileID = $_POST['ProfileID'];
  setcookie("ProfileID", $ProfileID, time()+3600, "/");
}
?>
```

The error I'm getting is with the $.post

----------


## Datacide

I have also tried this, but it didn't work:



```
$.ajax({ url: 'http://remotedomain/setcookie.php',
         data: {ProfileID: ProfileID},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});
```

Is it possible to execute a file on another server using jQuery? Maybe that's my problem - if so is there a workaround?

----------


## Datacide

Ok, I found a way, I think the issue was that it's a remote server. I found I can get around this using by claiming the remote file is JSON. It still returns an error if I check for it, but it works  :Smilie: 



```
callURL = 'http://remotedomain/setcookie.php?ProfileID=' + ProfileID;
$.ajax({
  url: callURL,
  dataType: 'jsonp',
  success:function(response){
    alert('Yay!');
  },
  error:function(XMLHttpRequest, textStatus, errorThrown){
  alert('Nay!');
}
});
```

----------


## Datacide

Hmm, seems I jumped the gun - IE isn't setting the cookie.

This is the code in setcookie.php:


```
<?php
$ProfileID = $_GET['ProfileID'];
setcookie("ProfileID", $ProfileID, time()+3600);
$data = '{}'; // json string

if(array_key_exists('callback', $_GET)){

    header('Content-Type: text/javascript; charset=utf8');
    header('Access-Control-Allow-Origin: http://www.example.com/');
    header('Access-Control-Max-Age: 3628800');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');

    $callback = $_GET['callback'];
    echo $callback.'('.$data.');';

}else{
    // normal JSON string
    header('Content-Type: application/json; charset=utf8');

    echo $data;
}
?>
```

This is the JavaScript code on the remote server:


```
var cnttime = new Date()
callURL = 'http://remotedomain/setcookie.php?ProfileID=' + ProfileID + '&timout=' + cnttime.getTime();
$.ajax({
            url: callURL,
            dataType: 'jsonp',
            cache: false,
            success:function(response){
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){                        
            }
    });
```

It works in Firefox & Chrome, but not IE  :Frown: 
Any thoughts?

----------


## Datacide

Kay, I resolved it myself.

Found answer here: http://stackoverflow.com/a/1211868

Basically, since the cookie is being set on a different server it's view as a third party cookie, which is generally blocked by IE default settings. Adding this line to setcookie.php corrected the issue:



```
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
```

----------

