Home > Backend Development > PHP Tutorial > Code to use cookies to count the number of times users visit web pages under PHP_PHP Tutorial

Code to use cookies to count the number of times users visit web pages under PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:38:08
Original
1087 people have browsed it

How to create a cookie?
The setcookie() function is used to set cookies.
Note: The setcookie() function must be placed before the tag.

Creating your first PHP cookie
When you create a cookie, using the function setcookie, you must specify three parameters. The parameters are setcookie(name, value, expiry):
Name: The name of your cookie. You will use this name to retrieve your cookies later, so don't forget it!
Value: The value stored in your cookie. The common values ​​are username (string) and last access time (date).
Expiration: The date on which the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and will be deleted when the browser is restarted.
In this example, we will create a cookie that stores the frequency of a user's last visit to measure people returning to our page. We want people to take more than two months to return to the site, so we will set the cookie expiry date two months in the future!

Copy code 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 can't follow the date calculations 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 cookie
If your cookie has not expired yet, let us retrieve it from that user's PC using the appropriate associative array named $_COOKIE. The name of your stored cookie is the key that will allow you to retrieve the value of your stored cookie!
Copy code The 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. Cookies are small files that a server leaves on a user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.

What should I do if my browser does not support cookies?
If your application involves browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One way is to pass the data from the form

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321801.htmlTechArticleHow to create cookies? The setcookie() function is used to set cookies. Note: The setcookie() function must be placed before the html tag. Creating your first PHP cookie When you create a cookie,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template