The PHP cookie records the Web page method that the user visited

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .mall array browser computer content cookie cookies create

Cookies are often used to identify users. Cookies are small files that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it sends cookies as well. Through the PHP tutorial, you can create and retrieve the value of a cookie.
How do I create cookies?
The Setcookie () function is used to set cookies.

Note: the Setcookie () function must precede the <html> label.

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


$content _id = Array ();//1. Create an array
$content _id[] = $_get[' ContentID ']; 2. Insert the ID into the array

if (Isset ($_cookie[' content_id '))//3. Determine if the cookie exists for the first time (if present)
{
$now _content = Str_replace ("", "", $_cookie[' content_id ']);//(4). You can look at the cookie, and if something goes wrong with unserialize, I'll remove the slash inside.
$now = unserialize ($now _content); (5). Serialize the string generated by the cookie in the array
foreach ($now as $n => $w) {//(6). There are a lot of elements, so I'm going to foreach out the value
if (!in_array ($w, $content _id))//(7). Determine whether this value exists, and if so, I do not insert into the array;
{
$content _id[] = $w; (8). Inserting into an array
}
}
$content = Serialize ($content _id); (9). Instantiate an array as a string
Setcookie ("content_id", $content, Time () +3600*24); (10). Insert INTO Cookie

}else {
$content = Serialize ($content _id)//4. Instantiate an array as a string
Setcookie ("content_id", $content, Time () +3600*24); 5. Generate Cookies
}

$getcontent = Unserialize (Str_replace ("", "", $_cookie[' content_id '));
/*foreach ($getcontent as $row => $r)
{
echo $r;//(value)
}*/

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.