Cookies and sessions in PHP

Source: Internet
Author: User
We will show you how to use cookies and sessions in PHP. There are two important functions: Cookies and Sessions. how are they used and what are the differences?

This article will show you how to use cookies and sessions.

Cookie introduction

Cookie is the data stored in the client browser. we track and store user data through cookies. Generally, the Cookie is returned from the server to the client through HTTP headers. Most web programs support Cookie operations,

Because the Cookie exists in the HTTP header, it must be set before other information output, similar to the use restrictions of the header function.

Cookie setting method

Setcookie ("name", 'hangsan ');

Setcookie ("name", 'hangsan', time () + 60); // Set the cookie validity period to 60 seconds.

// Setcookie ("visittime", date ("y-m-d H: I: s"), time () + 60); // Set the variable for saving the cookie expiration time

Cokie read method

$ Name = $ _ COOKIE ["name "};

Cookie deletion method

Setcookie ("name", "", time ()-1); // Set the cookie () time to the current time minus 1, time () the function returns the current timestamp in seconds. if the expiration time is reduced by 1 second, the previous time is obtained, and the cookie is deleted.

To delete cookiez, you only need to set the second parameter in the setcookie () function to a null value, and set the cookie expiration time of the third parameter to less than the current time of the system.

Finally, let's take a look at the biggest difference between session and cookie:

1. session stores session information on the server and transmits client information through a session ID. after the server receives the session ID, provide related sesion information resources based on this ID

2. cookies store all information on the client as text and are managed and maintained by the browser.

3. since session is stored on the server, all remote users cannot modify the content of the session file, and the cookie

For client-side storage, all sessions are much safer than cookies. of course, there are many advantages, such as easy control and custom storage (stored in the database )...

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.