PHP Cookie Practical Tutorial

Source: Internet
Author: User
two days ago we shared a cookie in Java to record the page last access time method, today we come to talk about how to implement the cookie in PHP , you can choose according to their favorite language!

//Control page display encoding
Header ("Content-type:text/html;charset=utf8");


//cookie record whether the page was first visited by the tag
$flag = false;
//First detect if there is a value for lastvisit this key in the cookie.
$lastVisit = isset ($_cookie[' lastvisit ') "$_cookie[' lastvisit ']: ';
if ($lastVisit) {

Setcookie () must be written before the Echo statement, or it will be due to HTTP reasons, PHP error

If you really want to put the Echo statement behind it, you need to turn on the OB cache
Setcookie (' lastvisit ', date (' y-m-d h:i:s ', Time ()), Time () +3600);
$flag = true;
Echo ' Your last access time: '. $lastVisit;
}
//Description is the first time you visit this page
if (! $flag) {
Setcookie (' lastvisit ', date (' y-m-d h:i:s ', Time ()), Time () +3600);
Echo ' Your first visit to this page! ';

}
?>

Today we mention a concept: OB caching. This knowledge point is very important, and very practical value, another day I will open a special course to talk about it, we look forward to! Share to this end, local tyrants feel good, just give it a reward !


The above describes the PHP cookie real-combat tutorial, including the content, I hope the PHP tutorial interested in a friend helpful.

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