cookie跨域問題

一、主域名相同。javascript

    例:bbs.college.com和www.college.comphp

    設置Domain爲:.college.com。
html

    PHP:
java

            在www.college.com/a.php中:setcookie("name","value","過時時間","path","domain");cookie

            在bbs.college.com/a.php中能夠直接讀取。
dom

    JS:
spa

            www.college.com/a.htmlhtm

                    <script type="text/javascript">  ip

                     function setCookie(c_name,value,expiredays) {  get

                     var exdate=new Date();  

                     exdate.setDate(exdate.getDate()+expiredays);

                     document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/;domain=college.com";  

                     }  

                     window.onload=function(){  

                     setCookie("college","woshiyigefenshuajiang",3600);  

                     alert("Cookie設置成功!");

                     }  

                     </script>

            bbs.college.com/a.html

                    <script>  

                     function getCookie(c_name) {

                     if (document.cookie.length>0)  

                     {

                     c_start=document.cookie.indexOf(c_name + "=");

                     if (c_start!=-1)

                     {   

                     c_start=c_start + c_name.length+1 ;

                     c_end=document.cookie.indexOf(";",c_start);

                     if (c_end==-1) 

                     c_end=document.cookie.length;

                     return unescape(document.cookie.substring(c_start,c_end));

                     }   

                     }  

                     return "";

                     }  

                     window.onload=function(){

                     var c_name="college";

                     if(getCookie(c_name)!=null){  

                     alert(getCookie(c_name));  

                     }

                     }  

                    </script>

二、主域名不一樣。

相關文章
相關標籤/搜索