PHP advanced tutorial: PHPCookies

Source: Internet
Author: User
PHP advanced tutorial: PHPCookies are often used to identify users.

What is Cookie?

Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same 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.

How to create a cookie?

The setcookie () function is used to set the cookie.

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

Syntax

Setcookie (name, value, expire, path, domain); example

In the following example, we will create a cookie named "user" and assign it "Alex Porter ". We also stipulate that the cookie will expire in one hour:

Setcookie ("user", "Alex Porter", time () + 3600 );
?>


Note: When sending a cookie, the cookie value is automatically URL encoded and automatically decoded during retrieval (to prevent URL encoding, use setrawcookie () instead ).
How can I retrieve the Cookie value?

The $ _ COOKIE variable of PHP is used to retrieve the cookie value.

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

// Print a cookie
Echo $ _ COOKIE ["user"];
// A way to view all cookies
Print_r ($ _ COOKIE );
?>

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



If (isset ($ _ COOKIE ["user"])
Echo "Welcome". $ _ COOKIE ["user"]. "!
";
Else
Echo "Welcome guest!
";
?>

How to delete a cookie?

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

Example of deletion:

// Set the expiration date to one hour ago
Setcookie ("user", "", time ()-3600 );
?>

What if the browser does not support cookies?

If your application involves a browser that does not support cookies, you have to use other methods to pass information from one page to another in the application. One way is to pass data from the form (the content about the form and user input is described earlier in this tutorial ).

The following form submits user input to "welcome. php" when the user clicks the submit button:





Retrieve the value in "welcome. php" as follows:


Welcome .

You are Years old.

The above is the PHP advanced tutorial: PHP Cookies. For more information, see PHP Chinese website (www.php1.cn )!

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.