简体   繁体   中英

Cookie page counter in php

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?

<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

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.

Use the preincrement operator instead, which increments and then returns:

$cookie = ++$_COOKIE['count'];

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.

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM