Application of Cookies in PHP

Source: Internet
Author: User

First, what is a cookie
A cookie is a mechanism for saving user information on a client browser, which is transmitted over the HTTP protocol

Ii. precautions in the use of cookies

1.cookie security is low and cannot be stored as confidential or important information in a cookie
2.cookie is limited in size and can only hold text information
3.cookie in the header of HTTP means that there is no HTML text output before the operation of the cookie in the PHP script

Third, the function method in the use of cookies

1.SetCookie function
Setcookie (name, value, expire, path, domain, secure);

Name---> Key name
Value---> Values
Expire---> Cookie expiration point in time (a standard UNIX time stamp)
Path---> Settings you can use the paths of cookie scripts, for example:/blog
Domain---> Set up a server that uses cookies to connect, for example: www.163.com
Secure---> Set whether to use HTTPS transport cookie by default without using HTTPS on setting to 1

Setcookie (' name ', ' Timor ', Time () +3600); ---> Set expiration point after one hours

Setcookie (' name ', ' Google ', Time () +3600, '/blog ', ' www.google.com ', 1);
Set the cookie to expire after one hours, the browser can only use HTTPS to pass cookies to the script file under www.google.com/blog

Cookie passing Array
Setcookie ("Arr[name]", ' Timor ', Time () +3600);
When receiving: $_cookie[' arr '][name]

2. Receiving cookies
In the script to use the cookie
Use $_cookie[' name '] to get the value of a COOKIE

Receives the value of an array
Setcookie ("Arr[name]", ' Timor ', Time () +3600);
When receiving: $_cookie[' arr '][name]

3. Delete Cookies

Setcookie (' name ', ' Timor ', Time () +3600);
Direct deletion: Setcookie (' name ');
Expire the cookie: Setcookie (' name ', ', Time ()-60);

Application of Cookies in PHP

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.