php php中的cookie页面计数器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7958930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookie page counter in php
提问by Steffan Harris
I am implementing a php page counter that will keep track of each time the user visits this page until the browser is closed. I am checking to see if the cookie is set, if it is. Then I am increment it and reset its value. But the problem I am having is that the counter is always at two, why is this?
我正在实现一个 php 页面计数器,它将跟踪用户每次访问此页面,直到浏览器关闭。我正在检查 cookie 是否已设置(如果已设置)。然后我增加它并重置它的值。但我遇到的问题是计数器总是在 2,这是为什么?
<html>
<head>
<title>Count Page Access</title>
</head>
<body>
<?php
if (!isset($_COOKIE['count']))
{
?>
Welcome! This is the first time you have viewed this page.
<?php
$cookie = 1;
setcookie("count", $cookie);
}
else
{
$cookie = $_COOKIE['count']++;
setcookie("count", $cookie);
?>
You have viewed this page <?= $_COOKIE['count'] ?> times.
<?php }// end else ?>
</body>
</html>
Edit: Thanks everyone, I did the pre increment thing and got it to work
编辑:谢谢大家,我做了预增量的事情并让它工作
采纳答案by jprofitt
This is happening because of the ++
being used as a post-increment instead of a pre-increment. Essentially what is happening is you're saying, "set $cookie
to the value of $_COOKIE['count']
, and then increment $_COOKIE['count']
. This means that each time you set it you're only actually making $cookie
equal 1, and even though $_COOKIE['count']
is showing it as 2, the actual cookie you send will only equal 1. If you do $cookie = ++$_COOKIE['count'];
you should get the correct result.
发生这种情况是因为它++
被用作后增量而不是前增量。本质上发生的事情是你说,“设置$cookie
为 的值$_COOKIE['count']
,然后递增$_COOKIE['count']
。这意味着每次设置它时,你实际上只是使$cookie
等于 1,即使$_COOKIE['count']
显示为 2,实际的 cookie你发送只会等于 1。如果你这样做,$cookie = ++$_COOKIE['count'];
你应该得到正确的结果。
回答by Blender
This line is the problem:
这一行是问题:
$cookie = $_COOKIE['count']++;
It doesn't increment the way you expect; the variable $cookie
is set to the value of $_COOKIE
, and then $_COOKIE
is incremented. It's the postincrement operator.
它不会像您期望的那样增加;变量$cookie
设置为 的值$_COOKIE
,然后$_COOKIE
递增。它是后增量运算符。
Use the preincrement operator instead, which increments and then returns:
改用预增量运算符,它会增加然后返回:
$cookie = ++$_COOKIE['count'];
回答by Marc B
the _COOKIE array is populated ONCE when the script first starts up (before any code is actually executed), and then is NOT touched again by PHP. Even if you do a setcookie() call to change one of the cookies, that change will NOT take effect until the next page load.
_COOKIE 数组在脚本首次启动时(在实际执行任何代码之前)填充一次,然后不再被 PHP 触及。即使您调用 setcookie() 来更改其中一个 cookie,该更改也不会在下一页加载之前生效。
As well, the ++ operator is working in "post-increment" mode. Doing
同样,++ 运算符在“后增量”模式下工作。正在做
$cookie = $_COOKIE['count']++;
boils down to this:
归结为:
$cookie = $_COOKIE['count'];
$_COOKIE['count'] = $_COOKIE['count'] + 1;
What you want is the PRE-increment version:
你想要的是预增量版本:
$cookie = ++$_COOKIE['count'];
which increments the cookie value and THEN assigns it to the cookie var.
这会增加 cookie 值,然后将其分配给 cookie 变量。
回答by Kris
You only need to do this
你只需要这样做
setcookie('count', isset($_COOKIE['count']) ? $_COOKIE['count']++ : 1);
Like so:
像这样:
<?php
setcookie('count', isset($_COOKIE['count']) ? $_COOKIE['count']++ : 1);
$visitCount = $_COOKIE['count'];
?>
<html>
<head>
<title>Count Page Access</title>
</head>
<body>
<?if ($visitCount == 1): ?>
Welcome! This is the first time you have viewed this page.
<?else:?>
You have viewed this page <?= $_COOKIE['count'] ?> times.
<?endif;?>
</body>
</html>