HTTP cookie 名称可以包含破折号吗?


Can HTTP cookie name contain dashes?

HTTP cookie 名称可以包含破折号吗?有可靠的参考吗?

我已经在Chrome上尝试过,这很好,但我担心其他浏览器可能不允许,因为在PHP参考中 http://php.net/manual/en/function.session-name.php 说:

name:
The session name references the name of the session, 
which is used in cookies and URLs (e.g. PHPSESSID). 
***It should contain only alphanumeric characters;*** 

cookie 名称在定义RFC6265的用语中定义为"令牌"。令牌在RFC2616 2.2 节中定义。以下为摘录:

   token          = 1*<any CHAR except CTLs or separators>
   separators     = "(" | ")" | "<" | ">" | "@"
                  | "," | ";" | ":" | "'" | <">
                  | "/" | "[" | "]" | "?" | "="
                  | "{" | "}" | SP | HT

如您所见,破折号 ("-") 不会出现在列表中,所以没关系。

规范说是,我的经验说不。

在 Ubuntu 上使用 Chromium,我今天在 cookie 中出现了一个非常奇怪的连字符问题,这让我想到了这个问题。 使用连字符会导致 cookie 在几微秒内过期(通过在页面加载中多次将 document.cookie 转储到控制台来证明)。

在扯掉了大部分头发后,我现在清楚地看到

tibbed-WP_ID_313=true; expires=Wed, 03 Dec 2014 20:28:26 GMT

(两天后)持续大约一半的页面加载,而这些变体

tibbedWP_ID_313=true; 
tibbed_WP_ID_313=true; 
tibbed:WP_ID_313=true;

都坚持了应有的状态。

在过去的几个月里,这段代码一直运行良好。 去图。 从现在开始,我将避免使用连字符(破折号)。