Simple use of cookies in PHP

Source: Internet
Author: User
Cookies and sessions are familiar, especially cookies. in browsers, we often need to save the logon information to the corresponding cookies.

Cookies and sessions are familiar, especially cookies. in browsers, we often need to save the logon information to the corresponding cookies.

Cookie is mainly used on the client in PHP, and session is used on the server. Like the header () function, no output is allowed before use.

The setcookie () function is used to set a cookie value. It has six parameters: cookie name, cookie value, storage time, storage path, valid domain name, and HTTP Secure transmission. Generally, you can simply set a cookie using the first three parameters.

For example: setcookie ("TestCookie", $ value, time () + 3600 );
When a cookie is used, the following cookie value is called: echo $ _ COOKIE ['testcookie ']. In addition, $ _ COOKIE: print_r ($ _ COOKIE) can be printed );

Sometimes the cookie is to be destroyed, just like setting the cookie value. The value is null and the expiration time is in advance: setcookie ("TestCookie", "", time ()-3600 );

Array usage: similar information can be saved as an array to cookies: setcookie (NAME [he], 'Man '); setcookie (NAME [she], 'Woman ');

Print array: print_r ($ _ COOKIE ['name']);

Note that the cookie value cannot be an array or a string.

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.