我在这样的免责声明php文件中设置了一个cookie ...
<script language = "JavaScript">
<!-- Begin hiding
function getCookieExpireDate(noDays){
var today = new Date()
var expr = new Date(today.getTime()+noDays*24*60*60*1000*365)
return expr.toGMTString()
}
function makeCookie(name, data, noDays){
var cookieStr = name + "="+ data
if (makeCookie.arguments.length > 2){
cookieStr += "; expires=" + getCookieExpireDate(noDays)
}
document.cookie = cookieStr
var hello="agreedterms.html";
window.location=hello;
}
function noway(){
var goodbye="index.html";
window.location=goodbye;
}
// End hiding -->
</script>
然后像这样在索引php中检查它....
<script language = "JavaScript">
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (cookieName) {
var arg = cookieName + "=";
var argLength = arg.length;
var cookieLength = document.cookie.length;
var i = 0;
while (i < cookieLength) {
var j = i + argLength;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal(j)
}
if (i == 0) {
break
}
}
return null;
}
if(getCookie('disclaimer') == null) {
location.href="disclaimer.php"
}
</script>
如果我使用mydomain.com访问该网站,这一切都可以正常工作 - 但如果我使用www.mydomain.com,那么它就无法正常工作。
任何想法我做错了什么?我是否需要以某种方式包含www?
答案 0 :(得分:1)
如果我记得你必须通过将以下内容附加到你的cookie字符串来设置cookie的域和路径
;domain=.domain.com;path=/
显然,您的域名替换了域名。域名前面的.
使其对您当前所在域之后的所有子域有效。
编辑: 另请参阅以下Stack Overflow答案:Creating a javascript cookie on a domain and reading it across sub domains