PHP Set cookie and Erase cookie method

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags access clear cookie cookies domain function html http

PHP Tutorial Setting cookies and cleaning cookies method
/*
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 requires your place to call this function before any output, including <html> and <head> tags, as well as any spaces.

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.

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 (' n ', ' www.jzread.com ');
Clear cookies
Clearcookies ();

Below Xing a support level two domain name cookie function.

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 Super-delegates access
$_cookie[$name] = $value;
Return Setcookie ($name, $value, $expire, $path, $domain, $secure, $httponly);
}
Call method

Setcookielive (' Webab ', ' 111CN ', Time () +86000, '/', ' jzread.com ');

One Entry level 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[ ' Cookies '] as $name => $value) {
        echo $name: $value <br/>n;
   }
}
 

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