PHP to use cookies to count the number of user access to the page code _php tips

Source: Internet
Author: User
Tags setcookie
How do I create cookies?
The Setcookie () function is used to set cookies.
Note: the Setcookie () function must precede the
Create your first PHP cookie
When you create a cookie, use the function Setcookie, you must specify three parameters. These parameters are Setcookie (name, value, expiration):
Name: Names of your cookies. You will use this name to retrieve your cookies later, so don't forget it!
Value: The value stored in your cookie. Common values are user names (strings) and the last Access time (date).
Expires: The cookie expires and is deleted when the date is expired. If you do not set this expiration date, 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 visit frequency to measure people's visit to our web pages. We want people to ignore more than two months to take a return to the site, so we will set the cookie's expiration date to two in the coming month!
Copy Code code as follows:

<?php
Calculate in 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 three key parameters: name, value and expiration date.
Retrieve your fresh cookies.
If your cookie has not expired, let us from that user's personal computer be using the appropriate associative array named $ _cookie. The name of your stored cookie is critical and will let you retrieve the value of your stored cookie!
Copy Code code as follows:

<?php
if (Isset ($_cookie[' lastvisit '))
$visit = $_cookie[' lastvisit '];
Else
echo "You ' ve got some stale cookies!";
echo "Your last visit is-". $visit;
?>

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. 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 other steps to pass information from one page to another in your application. One way is to pass data from a form
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.