colindunn
colindunn

Reputation: 3163

PHP dynamically generated hit counter

I am a total PHP novice and am trying to write what I think is a pretty simple script. This is the code I have so far:

HTML / PHP

<?php

$hits = file_get_contents('hits.txt');
 ++$hits;
file_put_contents('hits.txt', $hits);
echo $hits;

$url = $_GET['w'];
?>

<iframe src="<?php echo $url; ?>"></iframe>

<p>
<?php echo $hits; ?>
</p>

The result is a page with an iframe and a hit counter.

The problem with this script is that if the variable $url changes, the hit counter does not. My goal would be that if I visited http://www.website.com/index.php?w=blue.html I would get a different counter than if I visited http://www.website.com/index.php?w=yellow.html.

EDIT: I should add that this script is designed to accept any URL. I realize this complicates things significantly. My ultimate goal would be that if the counter didn't already exist for that particular URL, it would be generated on the fly.

Upvotes: 0

Views: 1886

Answers (2)

user1083975
user1083975

Reputation:

Your current code saves the hit points for every page to the same file as a simple string.

You have a number of options. Here's one that would work if you prefer to stick with text files instead of databases.

You could take the URL in, hash it, and save the counter for that page in a hash-named text file.

Something like

if( isset($_GET) && !empty($_GET['W']) ){
$url = md5($_GET['w']);
$hits = file_get_contents('/hit_counters/'.$url.'.txt');
$hits++;
file_put_contents('/hit_counters/'.$url.'.txt', $hits);
}

and then later you could echo out the hits under that or pull the hits in on another script and echo like that.

If you need it to create new ones on the fly, you could add something like

if(!is_file('/hit_counters/'.$url.'.txt')){
$fh= fopen('/hit_counters/'.$url.'.txt', 'w');
fwrite($fh, '1');
fclose($fh);
}

NOTE This could end up creating a ton of tiny text files, though. So be aware. If you are worried about that, you would really need to look into a database or read in a text file line by line to find the same hash.

TO IMPLEMENT Replace the top part of your code within the <?php ?> with the following:

if( isset($_GET) && !empty($_GET['W']) ){
    $url = md5($_GET['w']);
    if(!is_file('/hit_counters/'.$url.'.txt')){
        $fh= fopen('/hit_counters/'.$url.'.txt', 'w');
        fwrite($fh, '1');
        fclose($fh);
    }else{
        $hits = file_get_contents('/hit_counters/'.$url.'.txt');
        $hits++;
        file_put_contents('/hit_counters/'.$url.'.txt', $hits);
    }
}

This will take the page name in, hash it, check to see if /hit_counters/THEHASH.txt exists, and create it if not or add +1 to it otherwise. A hash is sort of like encryption, but not really. It will change your $_get['w'] into a longer random-looking string.

Upvotes: 4

swatkins
swatkins

Reputation: 13630

You're writing the same hits.txt file regardless of what $_GET['w'] is set to. Try putting the hits.txt file in a folder like this:

<?php

$url = $_GET['w'];

$dir = str_replace(".html", "", $url);

$hits = file_get_contents($dir.'/hits.txt');
++$hits;
file_put_contents($dir.'/hits.txt', $hits);
echo $hits;


?>

<iframe src="<?php echo $url; ?>"></iframe>

<p>
<?php echo $hits; ?>
</p>

Upvotes: 0

Related Questions