CrankBerry Blog Title
2010


(7) Comments

PHP text based counter – script specific no MySQL counter

Here's a counter script that can help you count downloads, specific pages, or overall general count. This is a simple function that works by storing text rather than using a MySQL database. Just for quick usage.

A sample of the results page:

Here's the function that you'll need for this to work.

function counter($counterName, $counterStage = 'store') {
        //Name
        $counter_name = preg_replace("/[^a-zA-Z0-9-]/", "", $counterName);
       
        //File
        $file = 'counter/counter_' . $counter_name . '.csv';
       
        //Get Time
        $now = strtotime("now");
               
        //IP
        $ip = $_SERVER['REMOTE_ADDR'];
       
        //Open File
    $handle = fopen($file, "a+");  
       
        //Store
        if ($counterStage == 'store') {
               
                //Store Array
                $storeArray = array($ip, $now);
               
                //Store
                fputcsv($handle, $storeArray);
       
        //Read
        } elseif ($counterStage == 'read') {
                //Row Counter
                $row = 0;
                $totalRow = 0;
               
                //Array
                $dispArray = array();
               
                while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
                        //Counter
                        $num = count($data);
                       
                        //Store To Array
                        if ($totalRow < 100) $dispArray[$row] = array($data[0], $data[1]);
                       
                        //Increase Row
                        $row++;
                        $totalRow++;
                }
               
                //Display
                echo '<h2>Counter Total For ' . $counterName . ': ' . $row . '</h2>';
                echo 'Recent 100 Counts (<a href="' . $file .'" target="_blank">Download full Stats</a>):

';
                echo '<table border="1" cellpadding="2" cellspacing="0">';
                echo '<tr>
                        <td width="20" style="font-size: 10px;"></td>
                        <td width="100" style="font-size: 10px;"><strong>IP</strong></td>
                        <td width="160" style="font-size: 10px;"><strong>Date</strong></td>
                        </tr>'
;
               
                //Sort
                krsort($dispArray);
               
                $rowCount = 1;
               
                //Displaying
                foreach ($dispArray as $row) {
                        echo '<tr>
                                <td style="font-size: 10px;">'
. $rowCount . '</td>
                                <td style="font-size: 10px;">'
. $row[0] . '</td>
                                <td style="font-size: 10px;">'
. date('M d, Y h:i a', $row[1]) . '</td>
                                </td>'
;
                               
                        //Count
                        $rowCount++;
                }
               
                echo '</table>';
        }

         fclose($handle);
}

To call the script to store this is the function to call:

counter('test');

The above case would store in a script called "test". If you're trying to do specific pages you can name your script home, about, or contact, whichever you please. To view results you'll call it with.

counter('test', 'read');

Same thing with the store function just change "test" to whichever counter you wish to retrieve.

TL
This entry was posted on Wednesday, September 29th, 2010 at 5:49 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
BL

7 Responses to “PHP text based counter – script specific no MySQL counter”

  1. NEIL NEIL says:


    CHEAP GENERIC PHARMACY : -==== Men's Health Drugs ====-

    Buy Cheap Generic Drugs Now!…

  2. ALEJANDRO ALEJANDRO says:


    GENERIC PHARMACY : -==== Thyroid Pills ====-

    Purchase Cheap Generic Drugs Now!…

  3. Interesting and amazing post..
    Thank you for the post..

  4. Intelisystem Intelisystem says:

    Hello,
    i have seen your blog . This blog gives knowledge about PHP text based counter

  5. Ellipsis Dive Ellipsis Dive says:

    Hi there. Nice blog. I really like your blog's design. :)

  6. cngothic cngothic says:

    thanks you, I was troubled for the MYSQL data statistics

Leave a Reply

Spam protection by WP Captcha-Free