I'm learning one of the most important concept of Cookies in PHP in detail.
While studying Cookies I come to know that "The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead)."
The above statement has created so many doubts in my mind which are as follows :
- 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" ?
- Why there is a need of another function like setrawcookie() when there is already a function setcookie() available for setting the cookie values?
- 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()
? - Can't the cookies be set like other variables like
$_COOKIE['cookie_variable'] = 'some_value'
instead of usingsetcookie()
orsetrawcookie()
?
If someone could clear all of my above mentioned doubts with perfect, suitable and easy to understand code examples along with the step-by-step crispy, lucid, easy to grasp explanation it would be of great great help to me.
Thanks.
It means that you don't have to worry about special characters.
Note that cookies are not a PHP concept; they're an extension to the HTTP protocol. And every protocol has a rigid structure that you need to comply with, or it simply wouldn't work. That structure relies on delimiters - characters, or sequences of characters that have special meaning assigned to them within that protocol.
It is inevitable that data transferred through every protocol will contain those special chracters, and that's why encoding is necessary.
For example, the semicolon (
;
) is used as a separator in theSet-Cookie
HTTP Header, so if your cookie value contains it, it needs to be encoded or otherwise the cookie wouldn't be properly parsed by browsers when they receive it.If you send a cookie with a value of
foo;bar
, without encoding, browsers will treat it as the valuefoo
with abar
flag attached to it.You would lose
;bar
as part of the data, and sincebar
is an unknown flag according to the protocol, browsers will simply ignore it, so you wouldn't even know that there was an error at all.PHP will automatically do the encoding when you set a cookie with
setcookie()
, and then automatically decode it when you read from the$_COOKIE
super-global.Mainly for 2 reasons:
The value you are sending may already be encoded.
You want to avoid double encoding, because at best it means you need to do more work. In the worst case, it may completely corrupt the data (i.e. you may never be 100% sure what the original data was).
There are many ways to encode values, and the one
setcookie()
uses may not be desirable.URL-encoding often encodes more data than the cookie format needs it to.
In extreme cases (you should never have to be concerned with this), since encoded data is usually larger in size the original, it may cause it to exceed the maximum cookie size (about 4kb). Or you may simply want to save bandwidth.
But is also not uncommon that you know 100% that the data doesn't need encoding, so you simply want to skip that unnecessary step.
Not in general, but this should already be answered above.
The drawback is that you need to encode the values yourself, if necessary.
The benefits are, again, already explained above.
setcookie()
leaves less room for errors from people unfamiliar with the cookie protocol.But that comes at a cost - the assumption that you always want URL-encoding. And assumptions are generally a bad thing in programming.
For a newbie,
setcookie()
is easier to use.For an expert,
setrawcookie()
puts less restrictions and is thus more flexible.Neither is inherently better, and since you mentioned "secure" - neither has any effect on security.
No.