iframe 跨域访问session/cookie丢失问题解决方法

今天因工做须要,在一个域名A的页面中,使用iframe包含另外一个域名B的页面。在chrome,firefox测试一切正常。javascript

当测试到IE7时,发现域名B中的页面session失效,不能写入session。
php


网上搜索后了解, 由于IE有安全策略,限制页面不保存第三方cookie(当前访问域名A下的页面为第一方cookie,而域名B下的页面为第三方cookie)。
html

虽然有安全策略限制,但咱们能够引入P3P声明容许第三方收集我的信息,使第三方cookie不被拒绝。
java


P3P:Platform for Privacy Preferences(隐私偏好平台)是W3C公布的一项隐私保护推荐标准,可以保护在线隐私权。使用Internet者能够选择在浏览网页时,是否被第三方收集并利用本身的我的信息。若是一个站点不遵照P3P标准,它的Cookies将被自动拒绝。
chrome


在iframe的页面头加入如下语句便可解决session失效问题。
跨域

[php] view plain copy 在CODE上查看代码片派生到个人代码片浏览器

  1. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');  安全


a.com/index.php
cookie

[html] view plain copy 在CODE上查看代码片派生到个人代码片session

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  2. <html>  

  3.  <head>  

  4.   <meta http-equiv="content-type" content="text/html;charset=utf-8">  

  5.   <title> domain A page </title>  

  6.  </head>  

  7.   

  8.  <body>  

  9.   <p>A Page</p>  

  10.   <iframe src="http://b.com/index.php"></iframe>  

  11.  </body>  

  12. </html>  


b.com/index.php

[php] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <?php  

  2. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');  

  3. session_start();  

  4. $_SESSION['code'] = md5(microtime(true));  

  5. ?>  

  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  7. <html>  

  8.  <head>  

  9.   <meta http-equiv="content-type" content="text/html;charset=utf-8">  

  10.   <title> domain B page </title>  

  11.  </head>  

  12.   

  13.  <body>  

  14.   <p>B Page</p>  

  15.   <?php  

  16.   if(isset($_SESSION['code'])){  

  17.     echo 'code:'.$_SESSION['code'];  

  18.   }  

  19.   ?>  

  20.  </body>  

  21. </html>  


IE iframe 跨域访问session问题解决了,但测试后发现,即便加入了P3P,safari浏览器依然不能保存iframe页面中的session。


原来safari的安全策略是,当cookie并未以第一方cookie保存过的(非iframe),将判断为不安全而直接拒绝。所以与IE的P3P有些不一样。


解决方法以下:

首先在iframe的页面中判断某个session值是否存在。若是不存在,使用js修改window.top.location跳到一个本域的setSession.php页面。

由于是用window.top.location打开,所以并不是iframe去访问,且能以第一方cookie保存.

而后在setSession.php页面执行完set session后,会跳回A域名的页面。以后就能使用session而不失效了。


代码以下:

a.com/index.php

[html] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  2. <html>  

  3.  <head>  

  4.   <meta http-equiv="content-type" content="text/html;charset=utf-8">  

  5.   <title> domain A page </title>  

  6.  </head>  

  7.   

  8.  <body>  

  9.   <p>A Page</p>  

  10.   <iframe src="http://b.com/index.php"></iframe>  

  11.  </body>  

  12. </html>  


b.com/index.php

[php] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <?php  

  2. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');  

  3. session_start();  

  4.   

  5. $ua = $_SERVER['HTTP_USER_AGENT'];  

  6. // 若是是safari  

  7. if(strstr($ua'Safari')!='' && strstr($ua'Chrome')==''){  

  8.     // 若是未设置第一方cookie  

  9.     if(!isset($_SESSION['safari'])){  

  10.         echo '<script type="text/javascript"> window.top.location="http://b.com/setSession.php"; </script>';  

  11.         exit();  

  12.     }  

  13. }  

  14.   

  15. $_SESSION['code'] = md5(microtime(true));  

  16. ?>  

  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  18. <html>  

  19.  <head>  

  20.   <meta http-equiv="content-type" content="text/html;charset=utf-8">  

  21.   <title> domain B page </title>  

  22.  </head>  

  23.   

  24.  <body>  

  25.   <p>B Page</p>  

  26.   <?php  

  27.   if(isset($_SESSION['code'])){  

  28.     echo 'code:'.$_SESSION['code'];  

  29.   }  

  30.   ?>  

  31.  </body>  

  32. </html>  


b.com/setSession.php

[php] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <?php  

  2. header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');  

  3. session_start();  

  4. $_SESSION['safari'] = 1;  

  5. header('location:http://a.com/index.php');  

  6. ?>  



源码下载地址:点击查看

相关文章
相关标签/搜索