Differences and connections between session and cookie in php

Source: Internet
Author: User
Tags session id sessions setcookie socket


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 ).

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.

<? Php
Setcookie ('site _ name', 'It blog '); // set a cookie: site_name. The value of site_name is the IT blog, so that a session is established, however, this cookie is stored in the memory and destroyed as the browser is closed.
Setcookie ('site _ name', 'It blog ', time () + 86400); // Set the cookie and set its expiration time to a day, this session will save the cookie value to the hard disk and close the browser. Within one day, the cookie is valid when the browser opens.
?> 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.

<? Php
Setcookie ('site _ name ','');
?> Cookie Destruction Method 2: Set the cookie value to the Expiration Time.

<? Php
Setcookie ('site _ name', 'It blog ', time ()-86400 );
?> Here you can see whether array_slice will cut the value in the cookie to reduce the cookie value?

The session is a server mechanism. The server uses a structure similar to a hash to store information, each visitor to a website is assigned a unique identifier for identification (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 contact:


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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.