Supponiamo di avere un set di cookie in first.com dire utente. Ora voglio leggere quel cookie in second.com tramite javascript e ajax. Ma non funziona. Ho xmlHttp.status = 0.come ottenere i cookie da un dominio diverso con php e javascript
esempio di codice
nel file readcookie.php secondo dominio
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function readcookie(){
createXMLHttpRequest();
xmlHttp.open("GET","http://www.first.com/cookie.php",true);
xmlHttp.onreadystatechange=getcookie;
xmlHttp.send(null);
}
function getcookie(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var reply=xmlHttp.responseText;
if(reply){
alert(reply);
}
}
else
alert(xmlHttp.status);
}
}
nel file cookie.php primo dominio
if(isset($_COOKIE['user'])){
echo $_COOKIE['user'];
}
else{
setcookie('user','a2345',0);
echo $_COOKIE['user'];
}
dare un'occhiata a http://stackoverflow.com/questions/402348/getting-setting-cookies-on-different-domains-with-javascript-or-other – Nobita
Possiamo organizzare che first.com specifichi che si tratta di cookie può essere letto da second.com? –