跨域共享cookie的原理是通过一个图片加载php文件的方法
1.
var expires_date = new Date( today.getTime() + (expires) );
domain_arr = ['mfancy.com','www.mfancy.it','www.mfancy.de','www.mfancy.es','www.mfancy.fr'];
this_expires = expires/1000;
//document.write("thisdomain:"+domain+"<br/>");
for( xx in domain_arr){
this_domain = domain_arr[xx];
//document.write("otherdomain:"+this_domain+"<br/>");
if(this_domain != domain){
this_url = "http://"+this_domain +"/" +"setcookie.php" + "?name="+name+"&value="+value+"&expires="+this_expires+"&path="+path+"&domain="+this_domain+"&secure="+secure;
var thisimg = new Image(1, 1);
thisimg.src = this_url;
}
}
在使用js设置cookie的时候,需要把这个值同步到其他域名,可以使用把这个值获取过来,然后通过图片加载php文件的方式
把这个值传递到其他网站的cookie中,然后,其他网站都可以获取到这个值,然后设置cookie,php的文件如下,这样就实现了各个网站共享cookie了。
2cookie.php<?php
if(isset($_GET['name']) && isset($_GET['value']) ){
$name = $_GET['name'];
$value = $_GET['value'];
$expire = $_GET['expire'] ? $_GET['expire'] : 3600 * 24 ;
$expire = time()+ $expire;
$path = $_GET['path'] ? $_GET['path'] : "/";
$domain = $_GET['domain'] ? $_GET['domain'] : "";
$secure = $_GET['secure'] ? $_GET['secure'] : "";
setcookie($name,$value,$expire,$path,$domain,$secure);
echo "success";
exit;
}
echo "fail";
?>
上面的代码的意思是,几个网站