Very good [Js]cookie mastery of the road 1th/2 page _javascript tips

Source: Internet
Author: User
Tags current time
If you have a bad memory like the author, you may not remember people's names at all. I met people, mostly just nodded, the question "Eat it!" "And expect greetings to end
。 If there is anything else to show, then I have to resort to some cunning tricks so that I can think of who the other is. Like talking about people who have something to do with each other, no matter how much they're connected.
Far, as long as you can avoid the embarrassment of not remembering each other's name: "What about the cute puppy Maifiste your next-door neighbor's nephew?" "Through this approach, I hope to make the other person feel that I am indeed
The fact that he or she was valued and even remembered the trivial things, although they actually forgot their names. But it's not that I don't take it seriously, but my memory is really bad, and the name to remember
There are so many words. If I could set cookies for everyone, I wouldn't be able to do the memory problem again.

In this article, we want to learn:

1. What is a cookie?
2. Composition of cookies
3. Manipulating Cookies
4. Cookie Monster

What is a cookie?

You ask, what is a cookie? A cookie is a small amount of data that the browser saves on the user's computer. It is associated with a particular Web page or Web site, automatically in a Web browser and
Passed between Web servers.

For example, if you run a Windows operating system and use Internet Explorer to surf the Internet, you will find a subdirectory under your Windows directory called
"Temporary Internet Files." If you have time to look at this directory, you will find that there are some files, the file name looks like an e-mail address. Like on my machine.
In this directory, there are "" files. This is a cookie file, where does this file come from? Guess, it comes from Microsoft's support station
Point. By the way, this is not my email address, hereby clarified.

Cookies are a great solution for managing small, unimportant details that you don't want to keep in a central database. (This is not to say that everyone's name doesn't matter.) For example, the eye
The ever-growing custom service on the previous site allows you to customize what they want to see for each user. If you are designing such a site, then how do you manage such information: a
The user likes the Green menu bar, while the other likes the Red one. It's really a tiring question. However, such information can be safely logged to the cookie and saved in the user's account
On the computer, and your own database space can be left to more long-term and meaningful data.

Fyi:cookies is often useful for security purposes. I don't want to go too far in this issue, just to provide an example of how to use a period of time expired
Cookies to ensure site security:

1. Use the username and password to log on through SSL.
2. Check the user name and password in the server's database. If the login succeeds, create a message digest (such as MD5) for the current time tag, and save it in the cookie and server number
According to the library. The user's logon time is saved in the user record in the server database.
3. In the event of each security transaction (any transaction in which the user is logged in), compare the message digest of the cookie with the summary saved in the server database, if the comparison fails,
The user is booted to the login interface.
4. If the 3rd step is passed, check that the current time and the sound of the logon time pass over the allowable length of time. If the user has timed out, lead the user to the login
5. If steps 3rd and 4th are passed, the login time is reset to the current time, allowing the transaction to occur. For those security sites that require you to log in, most probably use the
A similar approach is described in.
The composition of a cookie

Cookies were originally designed for CGI programming. However, we can also use JavaScript scripts to manipulate cookies. In this article, we'll show you how to use JavaScript scripts
To manipulate cookies. (if there is a need, I'll probably explain how to use Perl for cookie management in future articles.) But if there is no waiting, then I will teach you one hand:
Take a closer look at There is a cookie () function in this CGI package that you can use to create cookies. However, let us first introduce the nature of cookies.

In JavaScript scripts, a cookie is actually a string property. When you read the value of the cookie, you get a string of all the cookies used in the current Web page.
's name and value. Each cookie has four properties, in addition to the name and value values. These properties are: Expires expiration time, path path, domain field,
and secure security.

expires– Expiration time. Specifies the lifetime of the cookie. The exact value is the expiration date. If you want to have a cookie longer than the current browser session time, you must use this genus
Of When the expiration date is passed, the browser can delete the cookie file without any effect.

path– path. Specifies the Web page associated with the cookie. The value can be a directory, or a path. If Http:// has established a
Cookies, you can access the cookie in all pages in the and in any subdirectories in the directory below it. Say
, the cookies created by can be accessed on any page in the
But what if to access the Cookes of settings? At this time, we have to put
The cookie's path attribute is set to "/". When you specify a path, all Web pages that come from the same server with the same path in the URL can share cookies. Now look at another
One example: If you want share cookies, set the path to "
/devhead ".

domain– domain. Specifies the associated Web server or domain. Values are domain names, such as This is an extension of the path Path property. If we want to make can access cookies set by, what should I do? We can set the domain attribute to ""
and set the Path property to "/". FYI: You cannot set the cookie domain attribute to a different value than the domain of the server where it is set.

secure– safe. Specifies how the value of the cookie is passed between the user and the Web server over the network. The value of this property is either "secure" or empty. By default, the genus
Is null, that is, the use of unsecured HTTP connections to pass data. If a cookie is marked secure, it is passed to the Web server via HTTPS or another security protocol
Pass data. However, setting the secure attribute does not mean that other people cannot see cookies stored locally on your machine. In other words, setting cookies to secure only guarantees that cookies and web
The data transfer process between servers is encrypted and the cookie files stored locally are not encrypted. If you want to encrypt the local cookie, you have to encrypt the data yourself.

Manipulating cookies

Keep in mind that cookies are a string property of a document. To save a cookie, simply create a string, the format is name=<value> (name = value), and then the document's
The Document.cookie is set to be equal to it. For example, if you want to save the user name that the form receives, the code looks like this:

Document.cookie = "username" + Escape (form.username.value);

It is important to use the escape () function here because the cookie value may contain semicolons, commas, or spaces. This means that when you read the cookie value, you must use the corresponding
The unescape () function decodes the value.

We also have to introduce four properties of cookies. These attributes are appended to the string value in the following format:

name=<value>[; expires=<date>][; domain=<domain>][; path=<path>][; Secure]

Name =< value >[; expires=< date >][; domain=< domain >][; path=< path >][; security]

<value&gt, <date&gt, <domain> and <path> should be replaced with corresponding values. <date> should use the GMT format, which can be used in the JavaScript scripting language date class dates
The. toGMTString () method gets the date value of this GMT format. The square brackets represent this item to be optional. For example, the square brackets on both sides of [; secure] are meant to set cookies as safe
, you need to put "; Secure "is appended to the cookie string value. If "; Secure "is not added to the cookie string, then this cookie is not secure. Don't put
The angle brackets <> and brackets [] are added to the cookie (unless they are the contents of certain values). When you set properties, you do not have properties that can be set in any order.

Here is an example where the cookie "username" is set to expire after 15 minutes and can be accessed by all directories on the server, and can be ""
All servers in the domain are accessed and the security status is secure.
Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.