Php web Hitcounter

By DiGiTaL on Sep 15, 2008

Hello.

This is a hit counter I wrote in php...few weeks ago for a friend.

Cop that code and save it in a file called Counter.php, then go to whatever page you want show the hitcounter and put this code:

<?php  
   include("Counter.php");
   $counter = new Counter;
   $counter->add($counter->getinfo());
?>

^ Put that on the very top of your page and to display wherever you want your actual counter to show put this code:

<?php 
    $counter->display(); 
?>

If you want to just display Unique Hits put this:

   <?php
        $counter->display('u');
   ?>

This script also allows you to customize your counter box, so lets say you wanted to have red border, black background and yellow text on it:

  <?php
   $counter->styles = 'border: 1px solid red; background: black; color: yellow;';
   $counter->display();
?>

Note that the Styles propertie's values must be in CSS.

Let me know if you need help.

<?php

    /**
     * 
     * 
     * PHP Hit Counter using a flatfiles.
     * 
     * Navid "DiGiTaL" A - Aug 17, 08
     * 
     * 
     * 
     * 
     * Example: 
     * <?php
     *      include("Counter.php");
     *      $start = new Counter;
     *      $start->add($start->getinfo());
     * 
     * To display use:
     *      
     *      $start->display();
     * 
     * If you only want to display Unique visitors type:
     *      $start->display('u');
     * 
     * If you want to display all hits then type:
     *      $start->display();
     * 
     * To change the styles, change the CSS code for the variable $styles in the code
     * To change the datafile, change the $datafile variable to whatever you want.
     * 
     * 
     */

class Counter {

    //Name of the data file where we store all info
    private $datafile = "counter.txt";
    private $delimiter = "---";
    public $styles = "padding: 2px; color: #fff; background: grey; border: 1px solid blue; width: 80px; text-align: center;";

    function __construct() {
        if (!is_file($this->datafile)) {
            touch($this->datafile);
        }
    }

    public function add($info) {
        if (!$info) return;

        //open file for writing
        $file = fopen($this->datafile,'a+') or die ("Couldn't open file :(");

        if (!fwrite($file,nl2br($info."\n"))) {
            die("Couldn't Write to database :( ");
        }
    }

    public function getinfo() {
        //Get the vieweres info:
        $ip = $_SERVER['REMOTE_ADDR'];
        $page = $_SERVER['SCRIPT_NAME'];

        return $ip.$this->delimiter.$page.$this->delimiter.time().$this->delimiter;
    }

    public function display($views = 'a') {

        //Open and red the file in an array format
        $file = file($this->datafile) or die("File couldnt be read");

        //our counter variable
        $counter = 0;

        switch($views) {
            case 'a':
            case 'A':
            case 'all':
                $counter = count($file);
                break;
            case 'u';
            case 'U';
            case 'unique':
                //get all the IPs so we can compare them.
                //Im sure there is a better wayt to do this but we'll just stick to this for now
                $IP = array();
                foreach($file as $key => $value) {
                    $line = explode($this->delimiter,$value);
                    //echo $line[0];

                    if (!@array_values($IP,$line[0])) {
                        $IP[$line[0]] = TRUE;
                    } 
                }
                $counter = count($IP);
                break;
            default:
                return;
        }

        echo "<div style='{$this->styles}'>$counter hits</div>";
    }
}

?>

Comments

Sign in to comment.
Jonesy44   -  Sep 15, 2008

lost sorry, im used to simple PHP scripting xD

 Respond  
DiGiTaL   -  Sep 15, 2008

Hawkee: You like Analytics I like Mint :}. Its all good. Whichever works for us.

Jonesy44: Its the keyword to identify a property or method in OOP, unless it is a static property/variable then you access them via ::. Ie; Counter::styles.

 Respond  
Jonesy44   -  Sep 15, 2008

That's PHP? lol woah.. a lot diff to mystyle :p never heard of that -> stuff, what does it do? xD

 Respond  
Hawkee   -  Sep 15, 2008

Just looked at the mint demo and it's very limited compared to Analytics. What I like the most about Analytics is the ability to check visits to a specific page over a specified period of time. This is really helpful for determining growth of a new section of my site. And what's great is I can add these reports to my dashboard to get an overview of what's important to me as soon as I log in.

 Respond  
DiGiTaL   -  Sep 15, 2008

Because the person I wrote this little thing for wanted it to be seperate so he can modify his getinfo() method without touching the add(). He doesnt always parse the same paremeter to the add() method. He added to his code so there are differences. For example, I have just $this->getinfo(); in here but he had a few extra methods inside the getinfo method to seperate pages and the counting.

Yes, this will generate a big file if you just keep it running, a better thing to do would be to only save the count for Unique hits in which it will avoid all the page views.

There are better hitcounters and such on the internet, I've messed with google analytics, it is pretty good I should say but not the best. Its got some downfalls, I currently use Mint from http://www.haveamint.com/, check them out they have an amazing tool for web tracking.

 Respond  
Hawkee   -  Sep 15, 2008

Why not just do $info = $this->getinfo(); inside your "add" function rather than pass it as a parameter? Also, this won't scale very well if you get a lot of visitors, much better to use a database.

I don't think counters are very useful anymore considering all the stat software available like Google Analytics.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.