Home  >  Article  >  Web Front-end  >  Reasons why you should not use special characters in cookies_javascript tips

Reasons why you should not use special characters in cookies_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:23:13811browse

Constructs a cookie with a specified name and value.

The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.

The above is the description of the Cookie class constructor in the java API document, the key points The characters that can be used for name and value are introduced.

The parameter name needs to follow the specification RFC2109, which means that the name parameter can only contain letters or numbers, and cannot contain commas, semicolons, spaces, and dollar signs. The cookie's name attribute cannot be modified after it is set.

The parameter value can be any value set by the server, and the value can be reset through the setValue method.

But that’s not the case! !

(1) The value cannot contain commas. If the value contains commas, the program cannot be compiled.

If the value contains commas, as follows:

Cookie pwdCookie = new Cookie("option", "block1,block2");
The exception is as follows:

java.lang.IllegalArgumentException: block1,block2
In another article about JavaScript operating cookies mentioned:

Semicolons cannot be used in cookie names or values ​​( ;), comma (,), equal sign (=), and space.

In short, try not to use special characters in cookies.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn