Home > php教程 > PHP源码 > body text

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

WBOY
Release: 2016-06-08 17:22:13
Original
1186 people have browsed it

对于cookies读写操作我们如果域名配置没配置好那么可能只有一个域名要使用,像www.111cn.net与111cn.net后者是包括前者的哦,下面来给大家举个例子。

<script>ec(2);</script>

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

域名:111cn.net

浏览器:chrome34

代码:

 代码如下 复制代码
header("Content-type: text/html; charset=utf-8");
if (isset($_COOKIE['test']))
{
    echo '获取到的cookies是:'.$_COOKIE['test'];
}
elseif (setcookie('test', 'okh', time() + 3600, '/', '.111cn.net'))
{
    echo '设置cookies:test';
}
else
{
    echo '什么都没有';
}

用111cn.net访问,上面这段代码在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.111cn.net进行访问
修改之前的代码为
1
setcookie('test', 'okh', time() + 3600, '/', 'www.111cn.net')
这下正常设置,也正常记录了。
但是存在2个问题:

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

 代码如下 复制代码

var eTLDService = Components.classes["@mozilla.org/network/effective-tld-service;1"]
                    .getService(Components.interfaces.nsIEffectiveTLDService);
 
eTLDService.getBaseDomain(gBrowser.selectedTab.linkedBrowser.currentURI);
/*
www.111cn.net
*/

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


a.org
b.cn

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!