search
Homephp教程php手册php下用cookie统计用户访问网页次数的代码

利用cookie统计用户访问网页次数的代码,需要的朋友可以参考下。作为学习cookies的资料,不推荐使用。

如何创建 cookie?
setcookie() 函数用于设置 cookie。
注释:setcookie() 函数必须位于 标签之前。

创建您的第一个PHP cookie
当您创建一个cookie,使用函数setcookie,你必须指定三个参数。这些参数是setcookie(名称,值,过期):
姓名:您的Cookie的名称。您将使用此名称以后检索你的cookie,所以不要忘记它!
价值:在您的cookie中存储的值。共同价值观是用户名(字符串)和最后的访问时间(日期)。
到期:日期时,Cookie将过期,被删除。如果您没有设置此过期日期,那么它将被视为一个会话cookie被删除,并重新启动浏览器时。
在这个例子中,我们将创建一个Cookie,它存储用户上次访问频率来衡量人们回访我们的网页。我们希望人们忽视超过两个月采取返回到网站,所以我们将设置Cookie的到期日期中的两个未来个月!
代码如下:
//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);
?>

如果你不能走在这个例子中有所涉及的日期计算。重要的是你知道如何设置了一个cookie,通过指定的三个重要参数:名称,价值和到期日期。
检索你的新鲜曲奇
如果你的cookie的尚未到期的,让我们从该用户的个人电脑是使用恰当的关联数组名为$ _COOKIE中。你的存储cookie的名称是关键,将让您找回您的存储cookie的值!
代码如下:
if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
else
echo "You've got some stale cookies!";
echo "Your last visit was - ". $visit;
?>

cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。

如果浏览器不支持 cookie 该怎么办?
如果您的应用程序涉及不支持 cookie 的浏览器,您就不得不采取其他方法在应用程序中从一张页面向另一张页面传递信息。一种方式是从表单传递数据
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software