How PHP gets cookies

Source: Internet
Author: User
Tags send cookies setcookie

PHP Cookies

Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. This computer will send cookies whenever the same computer requests a page through a browser. With PHP, you can create and retrieve the value of a cookie.

How do I create cookies?

The Setcookie () function is used to set cookies. Note: the Setcookie () function must precede the
  1. <?php
  2. Setcookie ("User", "Alexporter", Time () +3600);
  3. ?>
  4. .....

Note: When a cookie is sent, the value of the cookie is automatically encoded and automatically decoded when it is retrieved. (To prevent URL encoding, use Setrawcookie () instead.) )

How do I get back the value of a Cookie?

PHP's $_cookie variable is used to retrieve the COOKIE's value. In the following example, we retrieve the value of the cookie named "User" and display it on the page:
  1. <?php
  2. Printacookie
  3. echo$_cookie["User"];
  4. Awaytoviewallcookies
  5. Print_r ($_cookie);
  6. ?>
In the following example, we use the Isset () function to confirm that a cookie has been set:
  1. <body>
  2. <?php
  3. if (Isset ($_cookie["user"))
  4. echo "Welcome". $_cookie["user". "! <br> ";
  5. Else
  6. echo "Welcomeguest!<br>";
  7. ?>
  8. </body>


How do I delete cookies?

When you delete a cookie, you should change the expiration date to a past point in time. Deleted instances:
  1. <?php
  2. Settheexpirationdatetoonehourago
  3. Setcookie ("User", "", Time ()-3600);
  4. ?>

How JavaScript creates, acquires cookies


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.