The difference between a cookie and a session, and how jquery operates cookies and session

Source: Internet
Author: User
Tags sessionstorage

The difference between a cookie and a session

Cookies, sometimes in their plural form, are the data (usually encrypted) stored on the user's local terminal by certain websites in order to identify the user and track the session. A session is a time interval between an end user communicating with an interactive system, usually the time elapsed between registering and logging out of the system. And, if necessary, there may be some room for operation.

How jquery operates cookies

A lightweight cookie plugin that can read, write, and delete cookies.

Configuration of the Jquery.cookie.js

The library file containing the jquery is first included, followed by the Jquery.cookie.js library file.

<script type= "Text/javascript" src= "Js/jquery-1.6.2.min.js" ></script>

<script type= "Text/javascript" src= "Js/jquery.cookie.js" ></script>

How to use

1. Add a new session cookie:

$.cookie (' The_cookie ', ' the_value ');

Note: When the cookie is not valid, the cookie is created by default until the user closes the browser, so it is called

"Session Cookie".

2. Create a cookie and set the validity time to 7 days:

$.cookie (' The_cookie ', ' The_value ', {expires:7});

Note: When a cookie is specified, the cookie created is referred to as a "persistent cookie (persistent cookie)".

3. Create a cookie and set a valid path for the cookie:

$.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/'});

Note: By default, only the Web page that sets the cookie can read the cookie. If you want a page to read a cookie set by another page, you must set the path to the cookie. The path to the cookie is used to set the top-level directory to read cookies. Set this path to the root of your Web site so that all Web pages can read cookies to each other (typically not set up to prevent collisions).

4. Read the cookie:

$.cookie (' The_cookie '); Cookie exists = ' The_value '

$.cookie (' not_existing '); Cookie does not exist = NULL

5. Delete the cookie by passing null as the value of the cookie:

$.cookie (' The_cookie ', null);

Explanation of----------related parameters---------------

1). expires:365

Defines the effective time of a cookie, which can be a number (from the time the cookie is created, in days) or a Date object. If omitted, the cookie created is a session cookie that will be deleted when the user exits the browser.

2). Path: '/'

By default, only the Web page where the cookie is set can read the cookie.

Defines a valid path for the cookie. By default, the value of this parameter is the path to the Web page where the cookie was created (the behavior of the standard browser).

If you want to access this cookie throughout the site you need to set a valid path: path: '/'. If you want to delete a cookie that defines a valid path, you need to include this path when calling the function: $.cookie (' The_cookie ', NULL,

{path: '/'});. Domain: ' example.com '

Default value: The domain name owned by the Web page that created the cookie.

3). secure:true

Default value: False. If the transport for True,cookie requires the use of security Protocol (HTTPS).

4). raw:true

Default value: False.

By default, cookies are encoded and decoded automatically when they are read and written (using encodeURIComponent encoding,

decodeURIComponent decoding). To turn this feature off, Raw:true can be set.

How jquery operates the session

Summary:

What we're sharing today is using jquery to deal with the session. We will use the Sessionstorage object, which is similar to the Localstorage object, except that Sessionstorage is used to store session data. This data will be erased when the user closes the browser.

Brief introduction:
Jquerysession is a jquery-based library of sessions that can be used to simplify our work. You need to introduce jquery before you can use it.

Grammar:

Add data
$.session.set (' key ', ' value ')

Delete data
$.session.remove (' key ');

Get Data
$.session.get (' key ');

Clear Data
$.session.clear ();

The difference between a cookie and a session, and how jquery operates cookies and session

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: info-contact@alibabacloud.com 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.