Hi all,

I am trying to make a script for maximal hit counter using below codes. So if user hit for bigger than 2, it would re assign the file of hitcounter.txt into "0" (as if in resetting) .. I tried this code but it does not work

<?php

//database SQL
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
//echo $hits[0];

if ($hits[0] =='1')
{
echo "1";
}

if ($hits[0] =='2')
{
echo "2";
}


if($hits[0] !='1'|$hits[0] !='2')
{
$hits[0] = '0';
}

Thank you for the tips

Dani AI

Generated

The root cause here is a combination of logic order and type/IO issues rather than a mysterious PHP limitation. correctly pointed out the operator problem, and gave a clean modulo solution — but two important gaps remain: (a) your script writes the file before you test/reset the count, so any reset never reaches disk, and (b) plain file reads/writes without locking will lose increments under concurrent hits. Also treat the stored value as an integer (use trim()/(int) or intval()) to avoid newline/string surprises from file().

Do this sequence: (1) open/read the file and convert to int, (2) increment and apply your reset rule (either if ($n>2) $n = 0; or use $n = ($n + 1) % 3;), then (3) write the new value back to disk. Always write after the reset check. Make sure the counter file exists and is writable by the web server (check permissions and path; use DIR to build a reliable path).

A safer, production-ready pattern uses file locking to avoid race conditions. Example (not the same code shown earlier in the thread):

$path = __DIR__ . '/hitcounter.txt';
$fp = fopen($path, 'c+'); // create if missing
if ($fp && flock($fp, LOCK_EX)) {
    rewind($fp);
    $raw = trim(stream_get_contents($fp));
    $count = (int)$raw;
    $count = ($count + 1) % 3;      // keep values 0,1,2
    rewind($fp);
    ftruncate($fp, 0);
    fwrite($fp, (string)$count);
    fflush($fp);
    flock($fp, LOCK_UN);
}
if ($fp) fclose($fp);

Troubleshooting: enable error reporting while testing, verify file path and permissions, inspect the file contents for stray whitespace, and log the intermediate $count before writing. For very high traffic, switch to a database or an atomic counter service instead of a plain file.

Recommended Answers

All 4 Replies

Member Avatar for Member #671080

Hi,

You are only using a single pipe symbol (|) when you should use 2.

Your current statement reads (if hits is not 1 OR hits is not 2) so only one of the conditions needs to be met and your counter will reset (if the counter is 1 it will match the "is not 2" part, if the counter is 2 it will match the "is not 1" part.

Try changing this

if($hits[0] !='1'|$hits[0] !='2')

to this

if($hits[0] != '1' && $hits[0] != '2')

This reads (if hits is not 1 AND hits is not 2) which will give you the result you expect.


Zagga

Hi thank you for the response,

but if I change it to &&, the value inside the text (hitcounter.txt) does not go to "0", but still in current change. Or is it not possible to change it programmitically?

Hi,

How about the following...

<?php
// Define hits count and hit counter file
$intHits = 0;
$strCountFile = 'hit_counter.txt';

// Get existing hits count from file, if file exists
if(file_exists($strCountFile) && is_readable($strCountFile)) {
    $intHits = (int)file_get_contents($strCountFile);
}

// Echo hits
echo $intHits;

// Increment hits - this will iterate through 0, 1, 2, 0, 1, 2
$intHits = ($intHits += 1) % 3;

// Write hits to file
file_put_contents($strCountFile, $intHits);
?>

Does this do what you want??

R.

Hi!

Sorry, I did not reply that soon! but your logic works superbly.. thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.