search
Homephp教程PHP源码php读写cookies无效问题解决办法

对于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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.