How to Read cookies in php

Source: Internet
Author: User
If the Cookie is successfully set, the client has a Cookie file to save the user information of the Web server. Suppose we use the Windows system on the client to browse the scripts on the server. if the Cookie is set successfully, the client has a Cookie file to save the user information of the Web server for a period of time. Suppose we use Windows to browse scripts on the server on the client. the Cookie file is stored in the "C: \ Documents Ents and Settings \ Username \ Cookies" folder. A Cookie records information in the form of a common text file. although you can directly open the browser using a text editor, it is meaningless to directly read the information in the Cookie file. When the customer visits the website again, the browser automatically sends all the Cookie information corresponding to the website to the server. After PHP5, any Cookie information sent from the client will be automatically saved in the $ _ COOKIE global array, therefore, each PHP script can read the corresponding Cookie information from the array. $ _ COOKIE global array stores all Cookie data content transmitted over HTTP, and uses the Cookie identification name as the index value and content value as the element.

In the Cookie setting script, the information read for the first time does not take effect. you must refresh or go to the next page to see the Cookie value, because the Cookie must be set to the client first, the system can only be sent when the request is accessed again. Therefore, to test whether a Cookie is successfully set, you can access its value through another page before it expires.

In PHP, you can directly read the COOKIE value of the browser through the Super global variable array $ _ Cookie.

The instance code is as follows:

 ';} Echo "the access time is:". date ('Y-m-d H: I: s'); // output the current access time?>

In the above code, first use the isset () function to check whether the Cookie file exists. If the Cookie does not exist, use the setcookie () function to create a Cookie and output the corresponding string. if the Cookie file exists, use the setcookie () function to set the file expiration time, and the time when the user visited the website last time. Finally, the current time of the website in the current location is displayed on the page.

When the instance is running for the first time, because the Cookie file is not detected, the running result is shown as follows:

Note: If no Cookie expiration time is set, the Cookie data will be automatically deleted when the browser is closed. If you set the expiration time for the Cookie, the browser will remember the Cookie data. even if you restart the computer, the data will be obtained when you access the website.

The above describes how to read the Cookie details in php. For more information, see other related articles in the first PHP community!

Related Article

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: 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.