1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Flat File Counter in PHP

Discussion in 'PHP' started by pradeep, Oct 17, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    This script is a simple file based counter. This is meant for sites that do not run a db and instead, use a file and a cookie. It isn't the best I've seen but it's simple. Consider this as a tutorial.

    Just create a file called counter.txt and put this where you want to display the counter.

    Remember to set the permissions on counter.txt so that your webserver can write to the file, CHMODing to 666 would be good enough.

    PHP:
    <?
       
    $visitor_ip $_COOKIE["user_ip"];
       
    $counter "counter.txt";
       
    $counter_file_line file($counter);
       
       if(!
    $vistor_ip)
       {
       
    setcookie("user_ip"$_SERVER['REMOTE_ADDR'], time()+360000);
       
    $counter_file_line[0]++;
       
    $cf fopen($counter"w+");
       
    fputs($cf"$counter_file_line[0]");
       
    fclose($cf);
       }
       elseif(
    $vistor_ip != $_SERVER['REMOTE_ADDR'])
       {
       
    $counter_file_line[0]++;
       
    $cf fopen($counter"w+");
       
    fputs($cf"$counter_file_line[0]");
       
    fclose($cf);
       }
       
    ?>
     
    Last edited: Oct 17, 2005

Share This Page