PHP use cookies to count the number of times users visit the Code _php tutorial

Source: Internet
Author: User
Tags setcookie
How do I create a cookie?
The Setcookie () function is used to set cookies.
Note: the Setcookie () function must be located in theLabel before.

Create your first PHP cookie
When you create a cookie that uses a function Setcookie, you must specify three parameters. These parameters are Setcookie (name, value, expiration):
Name: The names of your cookies. You will use this name to retrieve your cookie later, so don't forget it!
Value: The value stored in your cookie. Common values are the user name (string) and the last Access time (date).
Expiry: The date when the cookie expires and is deleted. If you do not set this expiration date, then it will be treated as if a session cookie is deleted and the browser is restarted.
In this example, we will create a cookie that stores the user's last access frequency to measure people's visit to our web page. We want people to ignore more than two months to take back to the site, so we will set the cookie expiration date to two months in the future!
Copy CodeThe code is as follows:
Calculate the Future
seconds * minutes * hours * days + current time
$inTwoMonths = * + * + time ();
Setcookie (' lastvisit ', date ("G:i-m/d/y"), $inTwoMonths);
?>

If you can't walk in this example, there is a date calculation involved. It is important that you know how to set up a cookie by specifying the three important parameters: name, value and expiration date.
Retrieve your Fresh Cookies
If your cookie is not yet expired, let us use the appropriate associative array name from the user's personal computer in _cookie. The name of your stored cookie is key and will allow you to retrieve the value of your stored cookie!
Copy CodeThe code is as follows:
if (Isset ($_cookie[' lastvisit '))
$visit = $_cookie[' lastvisit ');
Else
echo "You ' ve got some stale cookies!";
echo "Your last visit was-". $visit;
?>

Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it also sends a cookie. With PHP, you can create and retrieve the value of a cookie.

What if the browser does not support cookies?
If your application involves a browser that does not support cookies, you will have to take a different approach to passing information from one page to another in your application. One way is to pass data from a form

http://www.bkjia.com/PHPjc/321801.html www.bkjia.com true http://www.bkjia.com/PHPjc/321801.html techarticle How do I create a cookie? The Setcookie () function is used to set cookies. Note: the Setcookie () function must precede the HTML tag. Create your first PHP cookie when you create a cookie, ...

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