Hello, I am working on a counter which will record unique page views so that users can see how many times an advert has been visited like on ebay.

I have chosen to go the ip address route, as this seems to be more reliable than a cookie (would a cookie be better). However this means that I have to store every ip address that goes to every advert (up to 10k ads on the website) which could potentially come to a lot of data.

I found some code at http://www.trap17.com/index.php/php-unique-hit-counter_t8117.html which I intend to modify for the purpose:

<?php
//The file where IPs are logged.
$filename = "hits.txt";

//Writing the IPs to the file hits.txt
$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);

//Counting the hits from hits.txt.
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;
?>

I am going to convert this so that the data is saved in a database instead of a txt file. It doesn't seem very efficient to me that the number of unique ips is counted every time an advert is viewed. I guess it wouldn't take up much cpu power, but over 10k ads on a popular website could this become an issue?

Is this method the best way to do this, or is there a quicker, more efficient way that I have overlooked?

Thanks in advance.

    Chamelion wrote:

    Hello, I am working on a counter
    which will record unique page views
    so that users can see how many times

    an advert has been visited like on ebay.

    I found some code at http://www.trap17.com/index.php/php-unique-hit-counter_t8117.html which I intend to modify for the purpose:

    Is this method the best way to do this, or is there a quicker, more efficient way that I have overlooked?

    Thanks in advance.

    Hello.
    Me and a friend created a such script.
    to only count Unique IP once and never again.

    This is not really a hits counter,
    because them normal Counters can count same IP,
    if coming back after some time
    ( count again after 24 hours, or even 2 hours later or each new session ).

    We can call this script individual counter
    or more precise - Unique IP Visitors, perhaps.

    for example:
    If you write an article and put this article in one separate page
    and use this counter,
    you will see HOW MANY PEOPLE have read your article.
    At least how many did visit your article.

    Here is my script:

    <?php
    
    // Uncomment For Debug only!
    // error_reporting(E_ALL);
    
    $ipfile="data/iplist.php";  // PHP protected file, to write IP-numbers to
    $ip=$_SERVER[ 'REMOTE_ADDR' ];  // simple IP detector
    $view=0;         // if to Display Hits numbers result, see end of this script
    
    if(isset($_GET["view"]) && $_GET["view"]=="true")
      $view=1;
    
    if(!is_file($ipfile) || filesize($ipfile)<22)
    {
    $iplist=fopen($ipfile,"wb");
    fwrite($iplist,"<?php exit('no!'); ?>\n");
    fclose($iplist);
    $data="";
    }else{
    $iplist=fopen($ipfile,"rb");
    $data=fread($iplist,filesize($ipfile));
    fclose($iplist);
    $data=str_replace("<?php exit('no!'); ?>\n","",$data);
    }
    
    $visits=0;
    $found=0;
    if(!empty($data)){
    $ips=explode("\n",rtrim($data));
    $visits=count($ips);
    foreach($ips as $individual)
    {
    if($ip==$individual)
    $found=1;
    }
    }
    
    if(!$found)
    {
    $iplist=fopen($ipfile,"ab");
    fwrite($iplist,$ip."\n");
    fclose($iplist);
    $visits++;
    }
    
    if($view)
        print "Unique Visits: ".$visits;
    
    ?>

    I think you can figure out how it works.

    settings:
    $ipfile="data/iplist.php"; // file to write IP-numbers to
    $ip=$_SERVER[ 'REMOTE_ADDR' ]; // simple IP detectorr
    $view=0; // if to Display Hits numbers result, see end of this script

    To use you include in your index.php
    for example, add this line:
    include "ipcounter.php";

    Then if you call your "index.php" or directly the "ipcounter.php"
    with:
    http://yourURL/ipcounter.php?view=true
    .... you will get A DISPLAY of the result, of total number of unique hits.

    This is total number of IP-addresses stored in the 'iplist.php'
    Every IP number is written in a new line, in this logfile:
    12.12.13.145
    32.231.57.83
    212.87.77.3
    etc.
    etc.


    You will of course also be able to use 'iplist.php'
    for tracking who visited your page.
    And maybe also of some help if necessary to BAN some BAD HACKER 😃

    You can also find my script online in my web server PUBLIC FOLDER.
    alnog with some other small PHP Scripts I have written:
    http://okay.mine.nu/pub/
    http://okay.mine.nu/pub/ipcounter1a.phps


    Regards
    halojoy
    🙂

      what if a user uses dynamic ips?

        sid wrote:

        what if a user uses dynamic ips?

        yah, what if?

        Good question, mate.
        What do you think, yourself?

        Maybe it will destroy your important statistics!
        Make you sorry for rest of your life :eek:

        Like you mention,
        somebody using Dynamic IP, or PROXY surfing with dynamic IP
        will give some extra added records in 'iplist.php'
        But anyway, honestly,
        this counter of mine or something like this,
        gives more accurate information
        , although not 100% in all situations,
        than normal hitcounters - or services often used.
        And you give me some examples of PHP Scripts that cover EVERYTHING
        EVERY possible issue.

        30 lines of good php code
        would need maybe to be 10 times more, to ensure no Bug Reports or Security issues.
        And WILL slow your pages down
        .... with security comes always a delay somewhere or other kind of drawbacks:
        If you want some ---> You will pay for it, one way or another.

        If not with added complexity and size in your scripts,
        this delay will be at in The Web Server setup.


        Normal Addon counters we most often see around the web:
        ---> those have but one goal:

        To impress your visitors with a very high number
        with them, you can even sometimes add a suitable starting value
        for how many hits your website have had:
        for example enter: 53,219 visits
        ... to begin with when installed

        Respect
        halojoy
        🙂

          halojoy wrote:

          yah, what if?

          Good question, mate.
          What do you think, yourself?

          Maybe it will destroy your important statistics!
          Make you sorry for rest of your life :eek:

          i wasnt trying to be a smartass - this was an absolutely serious question. i have ran into this issue quite a few times. mostly back in the day 5-6 years ago, when it was really popular to display the page count on all kinds of sites. dynamic ips can mess up the stats considerably. especially on low-traffic sites.

            Did someone say dynamic IP + proxy + new-IP-every-pageload + shared-IPs?

            Er, I mean, did someone say AOHel... I mean AOL?

              Write a Reply...