is/are PHP 中的内置函数 setcookie() 和 setrawcookie() 的实际功能 difference/s 是什么?
What is/are the actual practical difference/s in functioning of the built-in functions setcookie() and setrawcookie() in PHP?
我正在详细学习 Cookies 中最重要的概念之一 PHP。
在学习Cookies的过程中了解到“cookie的值在发送cookie时会自动URL编码,并自动解码收到时(为防止 URL 编码,请改用 setrawcookie())。"
上面的说法让我产生了很多疑惑如下:
- 通过"The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received"实际发生了什么?
- 为什么在已经有函数 setcookie() 可用于设置cookie 值?
- 就是URL编码和URL解码的过程unsafe/harmful/hazardous/slow/anything 否则应该避免吗?
- 使用
setrawcookie()
优于 setcookie()
的 benefits/drawbacks 是什么?
- 哪个是safe/better/secure/reliable/etc。
setcookie()
或 setrawcookie()
?
- Cookie 不能像
$_COOKIE['cookie_variable'] = 'some_value'
等其他变量一样设置,而不是使用 setcookie()
或 setrawcookie()
吗?
如果有人能用完美、合适且易于理解的代码示例以及清晰、清晰、易于理解的逐步解释来消除我上面提到的所有疑问,那将对我有很大的帮助.
谢谢。
URL 编码将在 URLs/HTTP 中具有特殊含义的特定字符替换为百分比编码字符,例如space 变成 %20
。有关详细信息,请参阅 https://en.wikipedia.org/wiki/Percent-encoding。
如果您想设置一个您已经 URL 编码的 cookie,无论出于何种原因,您都需要 setrawcookie
。因此,如果您有一个已编码的 cookie,其中包含值 %20
,如果您使用 setcookie
,它将被编码为 %2520
;使用 setrawcookie
它将按原样保留并设置为 %20
。换句话说,setrawcookie
是一种 "just set the damn cookie, I know what I'm doing." 使用它来确保 cookie 格式正确符合 HTTP 字符编码标准的方法。
如果您首先需要有关编码或转义格式的更多背景信息,请参阅 The Great Escapism (Or: What You Need To Know To Work With Text Within Text)。
- What does actually happen practically by means of "The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received" ?
这意味着您不必担心特殊字符。
请注意,cookie 不是 PHP 概念;它们是 HTTP 协议的扩展。每个协议都有一个你需要遵守的严格结构,否则它根本行不通。该结构依赖于定界符 - 字符或在该协议中分配给它们的特殊含义的字符序列。
通过各种协议传输的数据不可避免地会包含这些特殊字符,这就是为什么需要编码的原因。
例如,分号(;
)在Set-Cookie
HTTP Header中用作分隔符,因此如果您的cookie值包含它,则需要对其进行编码,否则cookie不会浏览器在收到时正确解析。
如果您发送一个值为 foo;bar
的 cookie,如果不进行编码,浏览器会将其视为值 foo
并带有 bar
标志附在上面。
你会丢失 ;bar
作为数据的一部分,并且由于 bar
根据协议是一个未知标志,浏览器会简单地忽略它,所以你甚至根本不知道有错误.
PHP会在你用setcookie()
设置cookie时自动进行编码,然后当你从$_COOKIE
超全局读取时自动解码。
- Why there is a need of another function like setrawcookie() when there is already a function setcookie() available for setting the cookie values?
主要有两个原因:
您发送的值可能已经编码。
您想避免双重编码,因为充其量这意味着您需要做更多的工作。在最坏的情况下,它可能会完全损坏数据(即您可能永远无法 100% 确定原始数据是什么)。
编码值的方法有很多种,setcookie()
使用的一种可能并不理想。
URL-编码通常会编码比 cookie 格式需要的更多的数据。
在极端情况下(您永远不必担心这一点),由于编码数据通常比原始数据大,这可能会导致它超过最大 cookie 大小(大约 4kb)。或者您可能只是想节省带宽。
但您 100% 知道数据不需要编码的情况也很常见,因此您只是想跳过不必要的步骤。
- Is the process of URL encoding and URL decoding unsafe/harmful/ hazardous/slow/anything else so that it should be avoided?
一般不会,但是上面应该已经回答了。
- What are the benefits/drawbacks of using
setrawcookie()
over setcookie()
?
缺点是如有必要,您需要自己对值进行编码。
同样,上面已经解释了好处。
- Which one is safe/better/secure/reliable/etc.
setcookie()
or setrawcookie()
?
setcookie()
减少了不熟悉 cookie protocol.
的人犯错的空间
但这是有代价的——假设你总是想要 URL-编码。假设在编程中通常是一件坏事。
对于新手来说,setcookie()
更容易上手。
对于专家来说,setrawcookie()
限制较少,因此更灵活。
两者都不是天生的更好,而且既然你提到了“安全”——两者都对安全没有任何影响。
- Can't the cookies be set like other variables like
$_COOKIE['cookie_variable'] = 'some_value'
instead of using setcookie()
or setrawcookie()
?
没有
我正在详细学习 Cookies 中最重要的概念之一 PHP。
在学习Cookies的过程中了解到“cookie的值在发送cookie时会自动URL编码,并自动解码收到时(为防止 URL 编码,请改用 setrawcookie())。"
上面的说法让我产生了很多疑惑如下:
- 通过"The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received"实际发生了什么?
- 为什么在已经有函数 setcookie() 可用于设置cookie 值?
- 就是URL编码和URL解码的过程unsafe/harmful/hazardous/slow/anything 否则应该避免吗?
- 使用
setrawcookie()
优于setcookie()
的 benefits/drawbacks 是什么? - 哪个是safe/better/secure/reliable/etc。
setcookie()
或setrawcookie()
? - Cookie 不能像
$_COOKIE['cookie_variable'] = 'some_value'
等其他变量一样设置,而不是使用setcookie()
或setrawcookie()
吗?
如果有人能用完美、合适且易于理解的代码示例以及清晰、清晰、易于理解的逐步解释来消除我上面提到的所有疑问,那将对我有很大的帮助.
谢谢。
URL 编码将在 URLs/HTTP 中具有特殊含义的特定字符替换为百分比编码字符,例如space 变成 %20
。有关详细信息,请参阅 https://en.wikipedia.org/wiki/Percent-encoding。
如果您想设置一个您已经 URL 编码的 cookie,无论出于何种原因,您都需要 setrawcookie
。因此,如果您有一个已编码的 cookie,其中包含值 %20
,如果您使用 setcookie
,它将被编码为 %2520
;使用 setrawcookie
它将按原样保留并设置为 %20
。换句话说,setrawcookie
是一种 "just set the damn cookie, I know what I'm doing." 使用它来确保 cookie 格式正确符合 HTTP 字符编码标准的方法。
如果您首先需要有关编码或转义格式的更多背景信息,请参阅 The Great Escapism (Or: What You Need To Know To Work With Text Within Text)。
- What does actually happen practically by means of "The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received" ?
这意味着您不必担心特殊字符。
请注意,cookie 不是 PHP 概念;它们是 HTTP 协议的扩展。每个协议都有一个你需要遵守的严格结构,否则它根本行不通。该结构依赖于定界符 - 字符或在该协议中分配给它们的特殊含义的字符序列。
通过各种协议传输的数据不可避免地会包含这些特殊字符,这就是为什么需要编码的原因。
例如,分号(;
)在Set-Cookie
HTTP Header中用作分隔符,因此如果您的cookie值包含它,则需要对其进行编码,否则cookie不会浏览器在收到时正确解析。
如果您发送一个值为 foo;bar
的 cookie,如果不进行编码,浏览器会将其视为值 foo
并带有 bar
标志附在上面。
你会丢失 ;bar
作为数据的一部分,并且由于 bar
根据协议是一个未知标志,浏览器会简单地忽略它,所以你甚至根本不知道有错误.
PHP会在你用setcookie()
设置cookie时自动进行编码,然后当你从$_COOKIE
超全局读取时自动解码。
- Why there is a need of another function like setrawcookie() when there is already a function setcookie() available for setting the cookie values?
主要有两个原因:
您发送的值可能已经编码。
您想避免双重编码,因为充其量这意味着您需要做更多的工作。在最坏的情况下,它可能会完全损坏数据(即您可能永远无法 100% 确定原始数据是什么)。
编码值的方法有很多种,
setcookie()
使用的一种可能并不理想。URL-编码通常会编码比 cookie 格式需要的更多的数据。
在极端情况下(您永远不必担心这一点),由于编码数据通常比原始数据大,这可能会导致它超过最大 cookie 大小(大约 4kb)。或者您可能只是想节省带宽。
但您 100% 知道数据不需要编码的情况也很常见,因此您只是想跳过不必要的步骤。
- Is the process of URL encoding and URL decoding unsafe/harmful/ hazardous/slow/anything else so that it should be avoided?
一般不会,但是上面应该已经回答了。
- What are the benefits/drawbacks of using
setrawcookie()
oversetcookie()
?
缺点是如有必要,您需要自己对值进行编码。
同样,上面已经解释了好处。
- Which one is safe/better/secure/reliable/etc.
setcookie()
orsetrawcookie()
?
setcookie()
减少了不熟悉 cookie protocol.
但这是有代价的——假设你总是想要 URL-编码。假设在编程中通常是一件坏事。
对于新手来说,setcookie()
更容易上手。
对于专家来说,setrawcookie()
限制较少,因此更灵活。
两者都不是天生的更好,而且既然你提到了“安全”——两者都对安全没有任何影响。
- Can't the cookies be set like other variables like
$_COOKIE['cookie_variable'] = 'some_value'
instead of usingsetcookie()
orsetrawcookie()
?
没有