PHP Set cookie and Erase cookie method

Source: Internet
Author: User
Tags set cookie setcookie

/*
The Setcookie () function defines a cookie that is sent by the remainder of the HTTP header together. Like other headers, cookies must be sent from your script (this is a protocol limiting output). This will require your place to call this function before any output, including

Once the cookies have been identified, they can visit the next page loaded with $ _cookie or $ http_cookie_vars array. Note that the Super global variable $ _cookie form applies to PHP's 4.1.0. The value of the cookie also exists in the $ _request array.

The code is as follows Copy Code
BOOL Setcookie (String $name [, String $value [, int $expire = 0 [, String $path [, String $domain [, bool $secure = Fals e [, bool $httponly = FALSE]]]]

*/

The code is as follows Copy Code

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

The code is as follows Copy Code

Makecookie (' n ', ' www.111cn.net ');
Clear cookies
Clearcookies ();

Below Xing a support level two domain name cookie function bar.

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

  code is as follows copy code

setcookielive (' Webab ', ' 111CN ', Time () +86000, '/', ' 111cn.net ');

//One entry cookie Setup method

Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");

//After the page reloads, print them out
if (isset ($_cookie[' cookie ')) {
    foreach ($_cookie [' Cookie '] as $name => $value) {
        echo $name: $value <br/> ";
   }

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.