Giả sử tôi có một tập hợp cookie trong first.com cho biết người dùng. Bây giờ tôi muốn đọc cookie đó trong second.com thông qua javascript và ajax. Nhưng nó không hoạt động. Tôi đã có xmlHttp.status = 0.cách lấy cookie từ một tên miền khác với php và javascript
mẫu mã
trong lĩnh vực thứ hai tập tin readcookie.php
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);
}
}
trong file cookie.php miền đầu tiên
if(isset($_COOKIE['user'])){
echo $_COOKIE['user'];
}
else{
setcookie('user','a2345',0);
echo $_COOKIE['user'];
}
hãy xem http://stackoverflow.com/questions/402348/getting-setting-cookies-on-different-domains-with-javascript-or-other – Nobita
Chúng tôi có thể sắp xếp rằng first.com chỉ định rằng đó là cookie có thể được đọc bởi second.com? –