How to Set and clear cookies in PHP

Source: Internet
Author: User

PHP tutorial setting cookie and clearing cookie
/*
The setcookie () function defines the rest of the HTTP header sent by a cookie. Like other headers, cookies must be sent from your script before (this is a protocol limit output ). This requires any output before you call this function, including the <HTML> and

Once the cookies are determined, they can access the next page to load the $ _ cookie or $ http_cookie_vars array. Note that, for example, the super global variable $ _ cookie is applicable to PhP 4.1.0. The cookie value also exists in the $ _ Request array.

Bool setcookie (string $ name [, string $ value [, int $ expire = 0 [, string $ path [, string $ domain [, bool $ secure = false [, bool $ HTTPOnly = false])

*/

Function makecookie ($ var, $ value, $ life = 0, $ prefix = 0) // set cookie
{
Global $ cookiepre, $ cookiedomain, $ cookiepath, $ timestamp, $ _ server;
Setcookie ($ prefix? $ Cookiepre: ''). $ var, $ value,
$ Life? $ Timestamp + $ life: 0, $ cookiepath,
$ Cookiedomain, $ _ server ['server _ port'] = 443? 1: 0 );
}

Function clearcookies () // clear cookies
{
Global $ uid, $ username, $ PW, $ adminid;
Makecookie ('auth', '',-86400*365 );
$ Uid = $ adminid = 0;
$ Username = $ PW = '';
}

// Instance

Makecookie ('20140901', 'www .111cn.net ');
// Clear cookie
Clearcookies ();

// A second-level domain name Cookie function is supported below.

Function setcookielive ($ name, $ value = '', $ expire = 0, $ Path ='', $ domain = '', $ secure = false, $ HTTPOnly = false ){
// Set a cookie as usual, but also add it to $ _ cookie so the current page load has access
$ _ Cookie [$ name] = $ value;
Return setcookie ($ name, $ value, $ expire, $ path, $ domain, $ secure, $ HTTPOnly );
}

// Call Method

Setcookielive ('webb', '111cn', time () + 86000, '/', '111cn. net ');

// Entry-level cookie setting method

Setcookie ("Cookie [three]", "cookiethree ");
Setcookie ("Cookie [two]", "cookietwo ");
Setcookie ("Cookie [one]", "cookieone ");

// After the page reloads, print them out
If (isset ($ _ cookie ['cookies']) {
Foreach ($ _ cookie ['cooker'] as $ name => $ value ){
Echo "$ name: $ value <br/> N ";
}
}

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.