Cookie usage in PHP details _php tutorial

Source: Internet
Author: User
A cookie is a small file that is sent on the browser side, which can be used to record user-manipulated records, such as access to those files, etc.

Write Cookie

The code is as follows Copy Code

Setcookie (name, value, expire, path, domain);

function W_cookie ($n, $c, $e = 0, $isdes =1)
{
if ($isdes ==1) {$c =endes ($c, Deskey);}
$exp = time () + 3600 * 24 * 30;
if ($e = = 0)
{
Setcookie ($n, $c, $exp, "/");
}
Else
{
Setcookie ($n, $c, 0, "/");
}
}


The way to turn off cookies is to set it to expire.


Example 1

Write a cookie

The code is as follows Copy Code

function Cookie ($var, $value = ', $time =0, $path = ', $domain = ') {
$_cookie[$var] = $value;
if (Is_array ($value)) {
foreach ($value as $k = = $v) {
Setcookie ($var. ' ['. $k. '] ', $v, $time, $path, $domain, $s);
}
}else{
Setcookie ($var, $value, $time, $path, $domain, $s);
}
}

Calling methods

Cookies ("website", "Android Theme", "./", "www.hzhuti.com");
//

Example 2

Prevent duplicate Submissions

The code is as follows Copy Code

$time = time () + 300; Expires in 5 minutes
$code = MD5 ($string. $time. $salt);
Setcookie (' Check_time ', $time);
Setcookie (' Code ', $code);

Validation section
$TIME = time ();
if ($check _time < $TIME)//If the server time is greater than the validation time, the expiration date
Expire expired

if (MD5 ($string. $check _time. $salt)!== $code)

For more information on PHP cookie usage, refer to: http://www.bKjia.c0m/tags.php/php%20cookie/

http://www.bkjia.com/PHPjc/628924.html www.bkjia.com true http://www.bkjia.com/PHPjc/628924.html techarticle A cookie is a small file that is sent on the browser side, which can be used to record user-manipulated records, such as accessing those files such as the write cookie code below the copy code Setcookie (name, VA ... ).

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