PHP代码
1. <?php
2. header('P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"');//p3p
3. setcookie("TestCookie","test",time()+3600,"/",".a.com");//设置cookie
4. ?>
2. header('P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"');//p3p
3. setcookie("TestCookie","test",time()+3600,"/",".a.com");//设置cookie
4. ?>
再放置一个readcookie.php 内容:
PHP代码
PHP代码
1. <?php
2. echo $_COOKIE["TestCookie"];
3. ?>
2. echo $_COOKIE["TestCookie"];
3. ?>
然后在b.com下放置setcookie.php ,内容:
JavaScript代码
JavaScript代码
1. <script src="http://www.a.com/setcookie.php"></script>
然后访问http://www.a.com/setcookie.php 抓取数据包可以发现,在readlog.cn域下生成了一个cookie ,名称是TestCookie,值是test
下面访问http://www.b.com/readcookie.php 来验证一下,可以发现,cookie确实设置成功了。
有兴趣可以去掉header('P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"');这句,试一下就会发现cookie能设置,但是在b.com读不到这个cookie。
本文转自dotfun 51CTO博客,原文链接:http://blog.51cto.com/dotfun/286012