Jump to content

[SOLVED] instead of hits, i want unique hits (php counter)


ted_chou12

Recommended Posts

below is a counter php script:
[code]
<?php
$counter = ("$category/topic$number-counter.php");
$hits = file($counter);
$hits[0] ++;
$fp = fopen($counter , "w");
fputs($fp , "$hits[0]");
fclose($fp); ?>
[/code]
but it only records the hits, so the number gets very big, and not very practical at all...Can anyone suggest me how to change it to unique hits, so it only records 1 hit per IP Adress? thanks Ted
Link to comment
Share on other sites

okay, ive done it.
here
[code]
<?php
if ($_SESSION["forumtopic$number"] != "counted") {
$counter = ("$category/topic$number-counter.php");
$hits = file($counter);
$hits[0] ++;
$fp = fopen($counter , "w");
fputs($fp , "$hits[0]");
fclose($fp);
$_SESSION["forumtopic$number"] = "counted";}
?>
[/code]
Experts please check if i had done correctly,
begginers please dont hesitate to put them on your site, but remember *this is session counter, so same ip address can still be counted again after the session expires, however, i personally believe this is better than either simple counter, or unique counter, because session counter is my new invention...haha ;D
thanks
Link to comment
Share on other sites

not tested... but this should work

[code]
<?php
if($_COOKIE["forumtopic$number"] != "counted"){
$counter = ("$category/topic$number-counter.php");
$hits = file($counter);
$hits[0] ++;
$fp = fopen($counter , "w");
fputs($fp , "$hits[0]");
fclose($fp);
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie("forumtopic$number","counted",$inTwoMonths);
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.