problem with a counter function

I am having two separate problems. First, the counter function I’m using for a website periodically resets the value it stores in a text file on the website. I have not been able to figure out why this is. So I’ve been trying to do a workaround to minimize the effects of the counter resetting itself (basically, by periodically storing the value of the counter in the counter.php file, and then manually resetting the actual counter, as it doesn’t seem to reset itself unless the counter is quite high). However, I’ve run into a snag there as well - the variable I use to store the counter value is somehow not storing it as a numeric value.

The gist of it is that I’m storing a numeric value for a counter in a text file. But when I read it out of the text file into a variable, it treats it as a non-numeric value. I’m trying to figure out why this is and am getting nowhere.

Here are the pertinent sections of the php file:

<?PHP include("index.php"); $hitslog = "hits.txt"; // Get the hitslog file ready $hits = file($hitslog); $hits = $hits[0]; if (!isset($_COOKIE['visitcounter'])) { setcookie("visitcounter","site_reference", time()+60*60*24); $hits++; // Opening the hits file and write the number of hits: $fp = fopen($hitslog, "w"); flock($fp,LOCK_EX); fwrite($fp, $hits); fclose($fp); } echo("

"$hits." visitors since ".LAUNCHDATE." ^^

"); } ?>

LAUNCHDATE is a string constant marking the date that the website launched. As far as I can tell, it has no effect on the counter itself. hits.txt is the file I store the counter in.

I’ve used intval on it, and it returns 0. When I use strval, it returns the number stored in $hits as a string. When I use is_numeric, it returns false. I am having no luck figuring out why.

Hm, would be interesting to investigate why it resets. Don’t have time to look into it though. I just wanted to show you a simplified code that does the same :slight_smile:

[php]<?php

$hits = file_get_contents(‘hits.txt’);

if (!isset($_COOKIE[‘visitcounter’])) {
setcookie(‘visitcounter’,‘site_reference’, time()+606024);
file_put_contents(‘hits.txt’, ++$hits);
}
?>

<?= $hits ?> visitors since <?= LAUNCHDATE ?> ^^

[/php]

That works, thanks.

However, I need to be able to treat the value of $hits as a number (since it is a counter), and for whatever reason, it isn’t doing that, even though it clearly is a number.

Files are read as strings, but you can cast it to a number :slight_smile:

[php]<?php

$hits = (int) file_get_contents(‘hits.txt’);

if (!isset($_COOKIE[‘visitcounter’])) {
setcookie(‘visitcounter’,‘site_reference’, time()+606024);
file_put_contents(‘hits.txt’, ++$hits);
}
?>

<?= $hits ?> visitors since <?= LAUNCHDATE ?> ^^

[/php]

Now $hits is magically an integer, this works for other types as well (bool, object, float, etc)

The problem is when I try to cast it to a number like that, it comes out as 0 when I try to work with it later.

It shouldn’t, could you post an example where it is not working?

Actually, there’s a far more critical problem. When I use your code, the website gives me a fatal error because it’s trying to locate the counterOut function. So I can’t use it.

On top of that, without the flock command, the counter resets itself far more frequently. It just did it in the time it took me to write this post.

I would prefer not to risk messing up the website itself with further changes (I have offline backups, but it’s a pain having to put them in place), so I’m going to see if I can’t put up a simplified version of the code on a different site I run.

There’s no mention of a counterOut function anywhere in the code here…?

Regarding the counter resets, how many unique hits/minute do you have? In order to get multiple hits within milliseconds that has to be a lot, not even sure if that should lead to the problem as it should have been resolved with a lock.

Anyway, perhaps you could consider moving the counter to memory/database? It would be more suited for this kind of thing

Ah, damn, I left out part of my code.

function counterOut($hits) {
echo("<p class=“blogdate”>".$hits." visitors since “.LAUNCHDATE.” ^^

");
}

This, incidentally, is where I have problems with trying to treat $hits as an integer. For example, if I put something like “$hits += 500000;” (or “$hits = $hits + 500000”;), $hits becomes equal to 500000. I don’t know why it keeps doing that, but it was doing the exact same thing with your code segment.

I don’t have all that many hits. Something like 10,000 per month, though it’s been somewhat less over the past few months. And even with the lock, it doesn’t keep the counter from resetting every so often. My guess at this point is that the counter not being treated like an integer and the counter being reset every so often are related, but I don’t have anything to back that up.

Also, I’m afraid I don’t know how to move the counter to memory/database. My knowledge of PHP is limited, and while I can work with relatively simple code like this, I wouldn’t even know where to start to do something like that.

I am posting an updated version of the counter I’m trying to use.

<?PHP include("index.php"); $hitslog = "hits.txt"; // Get the hitslog file ready $hits = file($hitslog); $hits = $hits[0]; //echo("

$hits = ".$hits."

"); if (!isset($_COOKIE['visitcounter'])) { setcookie("visitcounter","Joushima Ayumi-chan", time()+60*60*24); // Opening the hits file and write the number of hits: $fp = fopen($hitslog, "w"); flock($fp,LOCK_EX); fwrite($fp, $hits++); flock($fp, LOCK_UN); fclose($fp); } // Text counter, print the number of hits function counterOut($hits) { $ctr = 500000; echo("

".$hits." visitors since ".LAUNCHDATE." ^^ (+".$ctr.")

"); } I have tried using the trim function. If I replace $hits = $hits[0]; with $hits = trim($hits[0]);, it still treats $hits as a non-numeric value, so if I add that to $ctr, I just get $ctr. I am not experienced enough in PHP to know what's wrong or how to fix it, and would appreciate any help you guys can give.

Color me annoyed…the problem was that the hits.txt file I was working with apparently had some invisible non-numeric characters in it (it was 8 bytes instead of 5), and thus what I pulled out of it was being treated as non-numeric. Replacing it with a different hits.txt file (using the same numeric value, but only 5 bytes) fixed the issue.

Sponsor our Newsletter | Privacy Policy | Terms of Service