Talking about PHP Cookie processing functions and phpcookie Functions

Source: Internet
Author: User
Tags set cookie setcookie

Talking about PHP Cookie processing functions and phpcookie Functions

(O too many records) o ☆[ BINGO!]

OK. Let's first look at what cookie is?

Cookie is a small file that the server stays on the client to identify users or store some data (note that session is stored on the server side, which is one of the differences between the two ). When logging on to a portal, we usually have the option "save login" or "automatically log on next time". When we select this option, the server will create a cookie file in our browser to save our information. When a computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the cookie value. Cookie is a very important role in the web. It was generated as early as the web browser. Cookies are often used for user verification systems.

1. Create a cookie

The setcookie () function can generate a cookie in PHP. Because the cookie is the content of the HTTP header, you must call setcookie () before outputting any data. This restriction is similar to header () and is defined:

bool setcookie( string name[,string value][,int expire][,string path][,string domain][,bool secure][,bool httponly])

Many parameters! A is not urgent. Let's take a look at the functions of each parameter:

Name: required, indicating the cookie name.

Value: Optional. It indicates the cookie value, which is stored on the client. If it is null, it indicates that the cookie information in the client is revoked (so that the cookie can be deleted ).

Expire: (optional) indicates the effective end time of the cookie, that is, the expiration time. If it is not specified or it is specified as 0, it is usually disabled when the browser is closed.

Path: Optional. Valid cookie path.

Domain: (optional) Valid cookie domain name.

Secure: Valid only during secure HTTPS transmission.

Instance (create a cookie named test with the value of China ):

<?phpsetcookie("test",China");?>

The value given by setcookie () can only be a number or string, and cannot be other complex structures.

2. Get cookie

After creating a cookie, you can use the predefined variable $ _ COOKIE to obtain the cookie. However, the cookie can only be obtained on other pages, because in php, the configured cookie does not take effect on this page unless the page is refreshed.

Instance:

<?php setcookie("test","China"); echo "cookie is ".$_COOKIE["test"]; ?>

The reason for refreshing the page is that the cookie value is not stored in In the c ookie variable, the cookie value appears only when the http request is sent to the server. _ COOKIE.

3. cookie Validity Period

A cookie has a lifecycle, that is, the validity period of the cookie. You can set the third parameter to set the validity period.

Instance (several methods for setting the cookie validity period ):

Setcookie ("cookie_one", "A", time () + 60*60); // setcookie ("cookie_two", "B", time () + 60*60*24); // setcookie ("cookie_three", "C", mktime (,) is invalid after one day )); // setcookie ("cookie_four", "D") expired at, January 1, October 9, 2020; // The cookie becomes invalid after the browser is disabled.

4. Valid cookie Path

The path in the cookie is used to control the path under which the set cookie is valid. The default value is '/'. It is valid in all paths, that is, it is valid in the whole server domain name, when other paths are set, they are only valid under the specified path and sub-path, for example:

setcookie('test', time(), 0, '/path');

The above settings will make test valid in/path and sub-path/abc, but the cookie value of test cannot be read in the root directory.

Generally, all paths are used. The path is set only when there are very few special requirements. In this case, the cookie value is passed only in the specified path, it can save data transmission, enhance security and improve performance.

5. Delete cookies

Deleting a cookie is simple. It is also implemented through setcookie () (do not use unset ()!!!) The following code is a simple example:

setcookie("test","");

You can delete a cookie by setting the second parameter to null. If a specific value is provided for the cookie, you still need to provide these parameters when deleting the cookie so that PHP can correctly Delete the cookie.

The above discussion about the PHP Cookie processing function is a small part of the Content shared to everyone, I hope to give you a reference, but also hope you can support the house of friends.

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.