Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should cookie values be URL encoded?

Tags:

When setting cookies, PHP url-encodes the cookie value (at least when not using setrawcookie) and it url-decodes the cookie value before making it available to the application in $_COOKIE.

Is this an accepted standard? If I set a raw cookie value of a%3Db, would I get back a=b in most web programming languages (through their respective cookie-reading mechanisms)?

like image 415
AndreKR Avatar asked Mar 10 '18 04:03

AndreKR


People also ask

What encoding type was used for the cookie value?

55951 – HTML5 specifies UTF-8 encoding for cookie values.

What needs to be URL-encoded?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

Are cookies base64 encoded?

Cookies are always base64 and JSON encoded #158.

How are cookies coded?

Cookies are plain text; they contain no executable code. A web page or server instructs a browser to store this information and then send it back with each subsequent request based on a set of rules.


1 Answers

sytech's answer (which I have accepted) is certainly correct as it quotes the spec, but since the spec is rather vague, here's an overview how some web frameworks actually handle the matter:

RFC6265:           "for example Base64" PHP:               URL encode Go:                raw Node.js + Express: URL encode 
like image 137
AndreKR Avatar answered Sep 22 '22 08:09

AndreKR