PHP development basic tutorial - Cookie

1. What are Cookies?

Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.


2. How to create a cookie?

The setcookie() function is used to set cookies.

Note: The setcookie() function must be located before the tag.

Syntax

setcookie(name, value, expire, path, domain);

Example: The code is as follows

In the following example, we will create a cookie named "user" and assign it the value "php". We have also specified that this cookie will expire after one minute:

You can also set the cookie expiration time in another way



3. How to retrieve the value of Cookie?

PHP’s $_COOKIE variable is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

The code is as follows

   php中文网(php.cn) 

In the following example, we use the isset() function to confirm whether the cookie has been set:

The code is as follows:

   php中文网(php.cn) 
"; else echo "普通访客!
"; ?>


4. How to delete cookies?

When deleting a cookie, you should change the expiration date to a point in time in the past.

Deleted instance:


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!