gpt4 book ai didi

php - 使用 cookie 显示用户在 PHP 中访问网站的次数

转载 作者:行者123 更新时间:2023-12-04 17:55:05 25 4
gpt4 key购买 nike

所以我对 PHP 还很陌生,我已经编写了我的第一个代码,主要是创建两个 cookie 来跟踪用户上次打开网站的时间和一个计数器。

我希望我的网站显示该人最后一次打开网站的时间,并带有一个计数器,上面写着“这是您访问过我们的 $cookieValue!”。

    <!DOCTYPE html>
<?php
$cookieValue = 1;

setcookie("time", $cookieValue, time()+(86400*365));

$cookieLastVisit = null;

setcookie("lastVisit", $cookieLastVisit, time()+(86400*365));
?>
<html>
<head>
<title>Question 2</title>
</head>
<body>
<?php
if (!isset($_COOKIE["time"])){
echo ("Welcome to my webpage! It is the first time that you are here.");

$visit = date(DATE_RFC1036);
setcookie("lastVisit", $visit);
}
else {
$cookieValue = ++$_COOKIE["time"];

echo("Hello, this is the " . $cookieValue . " time that you are visiting my webpage. Last time you visited my webpage on: " . $cookieLastVisit);

$visit = date(DATE_RFC1036);
setcookie("lastVisit", $visit);
}
?>
</body>
</html>

我已经完成了我的代码,但不明白为什么当我打开我的网站时,完全没有任何反应。

最佳答案

在将任何输出发送到浏览器之前,您需要将所有 setcookie() 函数调用放在页面顶部。

关于php - 使用 cookie 显示用户在 PHP 中访问网站的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40967415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com