跨域共享cookie

跨域共享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";
?>


上面的代码的意思是,几个网站

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值