教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php读写cookies无效问题解决办法

php读写cookies无效问题解决办法

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php读写cookies无效问题解决办法等资源,欢迎您收藏本站,我们将为您提供最新的php读写cookies无效问题解决办法资源
对于cookies读写操作我们如果域名配置没配置好那么可能只有一个域名要使用,像www.jiaochengji.com与jquerycn.cn后者是包括前者的哦,下面来给大家举个例子。

今天本地调试,有个cookies死活都写不进去,环境如下:

域名:jquerycn.cn

浏览器:chrome34

代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7073')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7073>header("Content-type: text/html; charset=utf-8");
if (isset($_COOKIE['test']))
{
    echo '获取到的cookies是:'.$_COOKIE['test'];
}
elseif (setcookie('test', 'okh', time() 3600, '/', '.jiaochengji.com'))
{
    echo '设置cookies:test';
}
else
{
    echo '什么都没有';
}

用jquerycn.cn访问,上面这段代码在chrome下一直设置成功,但是却一直都没记录。找了半天原因,一朋友从手册上告诉我:

The domain that the cookie is available to. Setting the domain to ‘www.example.com’ will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as ‘example.com’ will be available to higher subdomains, such as ‘www.example.com’. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

于是我做了这么一个调整:

用www.jiaochengji.com进行访问
修改之前的代码为
1
setcookie('test', 'okh', time() 3600, '/', 'www.jiaochengji.com')
这下正常设置,也正常记录了。
但是存在2个问题:

如何设置不带WWW的域名的cookies呢?比如:jquerycn.cn
设置www.jiaochengji.com虽然能在www.jiaochengji.com下使用,但是却不能在jquerycn.cn下使用,如何设置cookies使其通用呢?
通过求助,得知原因如下:
这个域名特殊。火狐、chrome不认为 www.jiaochengji.com 是 jquerycn.cn 的二级域名:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7227')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7227>
var eTLDService = Components.classes["@mozilla.org/network/effective-tld-service;1"]
                    .getService(Components.interfaces.nsIEffectiveTLDService);
 
eTLDService.getBaseDomain(gBrowser.selectedTab.linkedBrowser.currentURI);
/*
www.jiaochengji.com
*/

jquerycn.cn 在 effective_tld_names.dat 文件中列出来了:


a.org
b.cn

原来是这么一个小问题,困扰了我一天,原本是为了方便,所以将这个域名作为自己本地开发用,结果出现这个情况,只好将jquerycn.cn修改为1a.com

您可能感兴趣的文章:
php读写cookies无效问题解决办法
解决php session跨页面传递 session值丢失的问题(图文)
asp防止js或sql注入的方法详解
phpmyadmin不能登录,无任何提示的问题解决
PHP has encountered an Access Violation at 解决办法
Python爬虫进阶之会话和Cookies
ajax页面乱码与get post乱码的解决
扩展 Laravel 默认 Session 中间件导致的 Session 写入失效问题
Python3爬虫入门:高级用法
php-fpm出现Undefined variable几个解决办法

[关闭]
~ ~