跨域SESSION的问题

IE不允许跨域访问cookie(好象firefox没问题,ie自6.0以后改用w3c组织的P3P协议了.p3p是微软的隐私策略,通常情况下跨域iframe或者frameset默认采用的隐私策略为“中”,该级别的策略拒绝保留session。CAO PSA OUR则意味着你同意跨域保留session,但是也意味着你的网站不再安全。).

 

在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上可以找到很多相关的文章,如果网站可以采用设置Web.Config中的配置: mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="40"
/>
把cookieless="false"改成"true"就可以了但也同样有个小问题,就是如果页面中采用Javascript的window.location.href=''这样的方式来重定向的话,系统会认为这是另一个新的请求,产生一个新的SessionId,导致原Session同样的丢失所以对于重定向,还是使用Response.Redirect()为好

除了Ifrmae有丢Session问题外,frameset也有同样的问题Frameset的问题更不确定,是有时会丢,有时不会丢,这更认人头痛,在网上找到了一个方法,在页面page_onload里添加一语句:
Response.AddHeader("P3P","CP=CAO PSA OUR");
FrameSet中的Session丢失问题就解决了至于里面具体的原因 也没时间去搞懂了


最简单的方法就是在iis里设置

 

解决办法

response.addHeader("P3P","CP=CAO PSA OUR")。

不过难道我们需要在每个页面都加这个么?

不需要的

如果有权配置IIS服务器

打开IIS


管理工具——〉选择一个网站——〉属性——〉http头,增加一个http头
然后输入头名:P3P
输入头内容:CP=CAO PSA OUR

如果没有权限配置IIS服务器,但是你用的是asp.net的话

可以用httpmodual来实现在全部页面或者部分页面头部插入所需要的标志

 

其中CP=“XXX XXXX”这些是有具体含义的:
     CP就是compact policies的意思,
    另外header的值也可以是policyref="http://myhost/P3P/PolicyReferences.xml",就是指定一个策略文件。

具体请看这里.

下面是摘抄的一段Compact Policies的具体取值范围和设值含义。
 Compact Policies
Compact policies are essentially summaries of P3P policies. They can be used by user agents to quickly get approximate information about P3P policies, therefore improving performance.

For an in-depth explanation of compact policies, we refer to the P3P1.0[4] specification. Here, we limit to stating the syntax:

compact-policy-field  = `CP="` compact-policy `"`

compact-policy        = compact-token *(" " compact-token)

compact-token         =  compact-access           |

                                 compact-disputes         | 

                                   compact-remedies         | 

                      compact-non-identifiable |

                       compact-purpose          | 

                         compact-recipient        | 

                       compact-retention        | 

                       compact-categories       |

                        compact-test compact-access        = "NOI" | "ALL" | "CAO" | "IDC" | "OTI" | "NON"

                        compact-disputes      = "DSP"

                        compact-remedies      = "COR" | "MON" | "LAW"

                        compact-non-identifiable = "NID"

                        compact-purpose       = "CUR"        | "ADM" [creq] | "DEV" [creq] | "TAI" [creq] |

                         "PSA" [creq] | "PSD" [creq] | "IVA" [creq] | "IVD" [creq] |

                         "CON" [creq] | "HIS" [creq] | "TEL" [creq] | "OTP" [creq]                        

                           creq                  = "a" | "i" | "o"

                        compact-recipient     = "OUR" | "DEL" [creq] | "SAM" [creq] | "UNR" [creq] |

                                                             "PUB" [creq] | "OTR" [creq]

                        compact-retention     = "NOR" | "STP" | "LEG" | "BUS" | "IND"

                        compact-category      = "PHY" | "ONL" | "UNI" | "PUR" | "FIN" | "COM" | 

                                                            "NAV" | "INT" | "DEM" | "CNT" | "STA" | "POL" |

                                                             "HEA" | "PRE" | "LOC" | "GOV" | "OTC"

                        compact-test          = "TST"

class Session { //mysql的主机地址 const db_host = "localhost"; //需要第三方指定ip地址 //数据库用户名 const db_user = "root"; //需要第三方指定自己的用户名 //数据库密码 const db_pwd = ""; //需要第三方指定自己的库据库密码 //数据库 const db_name = "thinkphp"; //需要第三方指定数据库 //数据库表 const db_table = "tbl_session"; //需要第三方指定数据表 //mysql-handle private $db_handle; //session-lifetime private $lifeTime; function open($savePath, $sessName) { // get session-lifetime $this--->lifeTime = get_cfg_var("session.gc_maxlifetime"); // open database-connection $db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd); $dbSel = @mysql_select_db(self::db_name, $db_handle); // return success if(!$db_handle || !$dbSel) return false; $this->db_handle = $db_handle; return true; } function close() { $this->gc(ini_get('session.gc_maxlifetime')); // close database-connection return @mysql_close($this->db_handle); } function read($sessID) { // fetch session-data $res = @mysql_query("SELECT session_data AS d FROM ".self::db_table." WHERE session_id = '$sessID' AND session_expires > ".time(), $this->db_handle); // return data or an empty string at failure if($row = @mysql_fetch_assoc($res)) return $row['d']; return ""; } function write($sessID, $sessData) { // new session-expire-time $newExp = time() + $this->lifeTime; // is a session with this id in the database? $res = @mysql_query("SELECT * FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle); // if yes, if(@mysql_num_rows($res)) { // ...update session-data @mysql_query("UPDATE ".self::db_table." SET session_expires = '$newExp', session_data = '$sessData' WHERE session_id = '$sessID'", $this->db_handle); // if something happened, return true if(@mysql_affected_rows($this->db_handle)) return true; } else // if no session-data was found, { // create a new row @mysql_query("INSERT INTO ".self::db_table." ( session_id, session_expires, session_data) VALUES( '$sessID', '$newExp', '$sessData')", $this->db_handle); // if row was created, return true if(@mysql_affected_rows($this->db_handle)) return true; } // an unknown error occured return false; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值