解决cookie跨域访问的问题
网上关于cookie跨域访问的解决方法不少其中包括iframe,P3P等,本例以P3P为例。
在C:\Windows\System32\drivers\etc\hosts中添加如下代码
127.0.0.1 www.a.com
127.0.0.1 www.b.com
设置cookie的页面,a_setcookie.php
<?php 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 setcookie("test", $_GET['id'], time()+3600, "/", ".a.com"); var_dump($_COOKIE); ?>
测试获取cookie的页面,a_getcookie.php
<?php echo $_COOKIE["test"]; ?>
通过www.b.com/b_setcookie.php来访问www.a.com/a_setcookie.php中的cookie
<?php if($_SERVER['REQUEST_METHOD']=="POST") { echo $_REQUEST["name"]; $url='http://www.a.com:8080/ios/a_setcookie.php?id='.$_REQUEST["name"]; echo '<script src="'.$url.'"></script>'; //header('location:http://www.a.com:8080/ios/a_getcookie.php'); //此页面跳转不可行 echo '<script> window.location.href="http://www.a.com:8080/ios/a_getcookie.php";</script>'; } else { echo "<form method=POST action='b_setcookie.php' name='postform'>"; echo 'Name: <input type="text" name="name" size="15" maxlength="20" value="default" />'; echo "<a href='#' onclick=\"document.postform.submit();return false;\">登录</a>"; echo "</form><p>"; } ?>