Differences and connections between sessions and cookies

Source: Internet
Author: User
First, let's talk about cookies. Baidu Encyclopedia defines the data (usually encrypted) that a website stores on a local user terminal to identify users and track sessions ). Let's talk about cookies first.Baidu Encyclopedia defines the data stored on the user's local terminal to identify the user and perform session tracking (usually encrypted ).

The method for generating a cookie in php is simple: setcookie (name, value, expire, path, domain );

Name: The name defined in cookie.

Value: The value saved for name in the cookie. This value is stored on the user's terminal; do not store sensitive information (personal important information, money ).

Expire: the expiration time of the name value pair in the cookie. this value is saved as a timestamp.

Path: The valid path for storing the cookie value. if it is set to '/', it is valid for the entire project, if it is set to '/foo/', the valid path of the cookie is in foo and its subordinate files. if it is not set, its valid path is the current folder.

Domian: the range of available domain names for cookies.

 

 

 

This reminds me of the problems my colleagues encountered in the project. for example, if I have a feature that allows users to browse the first 10 pages, I want to display the pages used to prompt users to browse them, the cookie is used here. after the number of pages browsed by a user is greater than 10, only the first 10 are split. array_slice can help you, after array_slice is cut, does the page information saved in the cookie decrease? You may have this question. With this question, let's talk about the cookie destruction method.

Cookie Destruction Method 1: Set the cookie value to null.

 

 

 

 

Cookie Destruction Method 2: Set the cookie value to the Expiration Time.

 

 

 

Here you can see whether array_slice will cut the value in the cookie to reduce the cookie value?

Let's talk about sessionSession is a server mechanism. a server uses a structure similar to a hash to store information. each visitor of a website is assigned a unique identifier for recognition (session ID ). There are two storage formats: 1 is url transfer, 2 is cookie save.

Session_start () is the most important function in common session in php. if the session is not enabled, everything is free of discussion.

Php. ini also has some session settings, such:

Session. save_handler = files -- the session is saved as a file.

Session. save_path = "N;/path" --- save path of the session file. this path must be created by yourself and has the write permission. Where N;/path must be an integer so that session files can be stored in different directories, which is helpful for the server to process a large number of session files.

Difference between session and cookie:

1. the session is stored on the server. you can use session. save_path to set the location of the session file on the server.

2. the cookie is stored on the client and can be divided into temporary sessions (destroyed when the browser is closed, stored in memory) and persistent sessions (can be called continuously within the validity period and saved on the hard disk ).

3. the session is safer than the cookie, but a large number of sessions will also cause pressure on the server.

Session and cookie connection:

There are two effective methods for session execution: one is to interact with cookies, and the other is to pass through URLs.

Ps: sessionid is a very useful identifier that sometimes causes problems. on one occasion, over 50 requests are sent via ajax to request data through socket, and sometimes the data returned through the php socket is slow, although ajax is an asynchronous request, it is different on the php end. First, we will block the later request. the identifier is sessionid, this sessionid increases the user waiting time for php-side sequential processing, resulting in poor user experience. The solution is to use session_write_close () to disconnect the sessionid link.

 

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.