-
Nov 13th, 2012, 01:16 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Setting cookie on remote host
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:
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:
Code:
.error(function(jqXHR, textStatus, errorThrown) {
alert(textStatus + " | | + errorThrown);
});
But all that returns is "ERROR". Any thoughts/suggestions?
Last edited by Datacide; Nov 14th, 2012 at 07:25 AM.
PHP in your FACE!
-
Nov 13th, 2012, 02:06 PM
#2
Re: Setting cookie on remote host
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
-
Nov 13th, 2012, 02:10 PM
#3
Thread Starter
Hyperactive Member
Re: Setting cookie on remote host
The cookie is set in the remote PHP file:
Code:
<?php
if ($_POST['ProfileID']) {
$ProfileID = $_POST['ProfileID'];
setcookie("ProfileID", $ProfileID, time()+3600, "/");
}
?>
The error I'm getting is with the $.post
-
Nov 13th, 2012, 03:39 PM
#4
Thread Starter
Hyperactive Member
Re: Setting cookie on remote host
I have also tried this, but it didn't work:
Code:
$.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?
Last edited by Datacide; Nov 13th, 2012 at 04:06 PM.
PHP in your FACE!
-
Nov 13th, 2012, 04:04 PM
#5
Thread Starter
Hyperactive Member
Re: Setting cookie on remote host
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
Code:
callURL = 'http://remotedomain/setcookie.php?ProfileID=' + ProfileID;
$.ajax({
url: callURL,
dataType: 'jsonp',
success:function(response){
alert('Yay!');
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert('Nay!');
}
});
-
Nov 13th, 2012, 10:23 PM
#6
Thread Starter
Hyperactive Member
Not working in ie
Hmm, seems I jumped the gun - IE isn't setting the cookie.
This is the code in setcookie.php:
Code:
<?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:
Code:
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
Any thoughts?
Last edited by Datacide; Nov 14th, 2012 at 07:42 AM.
PHP in your FACE!
-
Nov 14th, 2012, 07:45 AM
#7
Thread Starter
Hyperactive Member
Re: Setting cookie on remote host
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:
Code:
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|