Code for counting the number of times a user accesses a webpage using cookies in php

Source: Internet
Author: User
Use cookies to count the code of the number of times a user accesses a webpage. For more information, see. It is not recommended for cookies. How to create a cookie?
The setcookie () function is used to set the cookie.
Note: The setcookie () function must be locatedBefore the tag.

Create your first PHP cookie
When you create a cookie and use the setcookie function, you must specify three parameters. These parameters are setcookie (name, value, expiration ):
Name: the name of your Cookie. You will use this name to retrieve your cookie, so do not forget it!
Value: the value stored in your cookie. The common values are the username (string) and the last access time (date ).
Expiration: the Cookie expires and is deleted. If you do not set this expiration date, it will be treated as a session cookie being deleted and restarted when the browser is restarted.
In this example, we will create a Cookie that stores the frequency of the user's last visit to measure people's visits to our webpage. We want people to ignore taking over two months to return to the website, so we will set the Cookie expiration date for the next two months!

The code is as follows:

// Calculate 60 days in the future
// Seconds * minutes * hours * days + current time
$ InTwoMonths = 60*60*24*60 + time ();
Setcookie ('lastvisit', date ("G: I-m/d/y"), $ inTwoMonths );

If you cannot go through the date calculation involved in this example. It is important that you know how to set a cookie by specifying three important parameters: name, value, and expiration date.
Retrieve your fresh cookies
If your cookie has not expired, let's use the appropriate associated array name $ _ COOKIE from the user's PC. The key is the name of your stored cookie, which will allow you to retrieve the value of your stored cookie!

The code is as follows:

If (isset ($ _ COOKIE ['lastvisit'])
$ Visit = $ _ COOKIE ['lastvisit'];
Echo "You 've got some stale cookies! ";
Echo "Your last visit was-". $ visit;

Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the cookie value.

What if the browser does not support cookies?
If your application involves a browser that does not support cookies, you have to use other methods to pass information from one page to another in the 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: 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.