问题描述
我尝试在cookie名称中使用括号。
它应该看起来像这样(这是浏览器想要的!):
名称:name [123456] .newName
内容:20
以下是我的示例:
$ cookie = name [123456] .newName = 20
但是当我分析浏览器看到的内容时,这:
cookie ['name'] = Array
我想:
cookie ['name [123456] .newName '] = 20
我的问题是:如何以浏览器的方式
提前谢谢。
解决方案你必须这样做:
<?php
setcookie('name [123456] .newName',20);
?>
这将生成以下标题:
Set-Cookie:name [123456] .newName = 20
...和浏览器(至少是Firefox)似乎处理得很好。
当你想读取值时,问题开始。 PHP有一个不错的功能:每当它找到一个输入参数(get,post,cookie ...)在其名称上加上方括号,它将从中构建一个数组。因此
print_r($ _ COOKIE)
显示:b(
[name] => Array
(
[123456] => 20
)
)
我不知道有任何方法禁用此功能,因此您可能需要使用字符串函数并解析原始Cookie的内容,可在
$ _ SERVER ['HTTP_COOKIE']
中找到:name [123456] .newName = 20
I'm trying to use brackets within the name of a cookie.
It is supposed to look like this(this is how the browser wants it!):
Name: name[123456].newName Content: 20
Here is my example:
$cookie = "name[123456].newName=20"
But when I analyze what the browser sees, I get this:
cookie['name'] = Array
And I want:
cookie['name[123456].newName'] = 20
My question is: How should I write the cookies name in a way that the browser understands?
Thank you in advance.
解决方案Actually, all you have to do is this:
<?php setcookie('name[123456].newName', 20); ?>
This generates the following header:
Set-Cookie: name[123456].newName=20
... and browsers (well, at least Firefox) seem to handle it just fine.
The issue starts when you want to read the value back. PHP has an otherwise nice feature: whenever it finds an input parameter (get, post, cookie...) with square brackets on its name, it'll build an array from it. So
print_r($_COOKIE)
displays this:Array ( [name] => Array ( [123456] => 20 ) )
I'm not aware of any way to disable this feature so you probably need to use string functions and parse the contents of the raw cookie, which can be found at
$_SERVER['HTTP_COOKIE']
:name[123456].newName=20
这篇关于在Cookie名称中使用括号 - PHP为什么将它转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!