DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > PHP
Reload this Page Simple Hit counter
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Oct 2008
Default Simple Hit counter - 10-04-2008, 04:35 AM

A hit counter will let us know how many times a page is accessed. In case one visitors loads the page several times, the hit counter will increase several times (but this is likely to happen only a few times).

The code for the hit counter bellow will save the number of hits in a file named counter.txt (the name of this file may be changed). Each time the page is loaded, the file will be read, the number will be increased by one and the new number will be saved to the same file.

<?php

//The file where number of hits will be saved; name may be changed; p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";

// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits = fgets($fp,100);
fclose($fp);

//increading number of hits
$hits++;

//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);

//printing hits; you may remove next line (and keep the counter only for your records)
print $hits;

?>
To use this code, copy it to your page in the exact position where you want to show number of hits. Feel free to reply to this thread and add to this list, it will be helpful for everyone!

web application development

Outsourcing software development
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Sep 2008
Default Another hit counter solution - 03-27-2009, 03:00 PM

PHP Code:
<?php
/*
 * Use single quotes ('') for strings that do not have
 * escaped characters or embedded variables.
 */

// Path to the counter file
$path 'hits_count.txt';

// Initializes $hits for echo in case fopen() fails
$hits '';

/*
 * Opens the file only once to change the counter.
 * 
 * If you open and close the file twice, in the unlikely
 * scenario where a second visitor reads the file while a
 * first visitor is incrementing the counter, the number
 * that the second visitor writes to the file will be the
 * same as the number the first visitor just wrote.
 * 
 * 'a+' opens the file for reading and writing and
 * attempts to create it if it does not exist. The pointer
 * is placed at the end of the file.
 */
if ($fh fopen($path'a+'))
{
    
/* 
     * Obtains exclusive write access; Does not prevent
     * reading or writing if a lock cannot be acquired
     */
    
flock($fhLOCK_EX);
    
    
// Returns the pointer to the beginning of the file
    
fseek($fh0);
    
    
// Stores the first line in $hits
    
if ($hits fgets($fh))
    {
        
// Cleans up white space and increments counter
        
$hits trim($hits) + 1;
    }
    else
    {
        
// Starts a new counter
        
$hits '1';
    }
    
    
/*
     * To test this script, create two copies: a.php and b.php.
     * In only a.php, uncomment sleep().
     * Run a.php.
     * Run b.php before a.php finishes.
     * Notice how b.php has to wait for a.php to close the file.
     */
    //sleep(10);
    
    // Removes old count
    
ftruncate($fh0);
    
    
// Inserts new count
    
fwrite($fh$hits);
    
    
// Closes the file handle and releases the lock
    
fclose($fh);
}

// Echo is minimally more efficient than print.
// Optional statement
echo $hits;

?>
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Sep 2008
Default Yet another hit counter solution - 03-27-2009, 03:04 PM

The script I posted before this is more reliable, but here is a short solution.
PHP Code:
<?php
// An attempt at a short solution

// Path to the counter file
$path 'another_counter.txt';

// If the file exists, gets the counter and increments it
// If the file does not exist, sets the counter to 1
$hits file_exists($path)
      ? (
trim(file_get_contents($path)) + 1)
      : 
1;

// Updates the counter
file_put_contents($path$hits);

// Shows the counter
echo $hits;

?>
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple JMX monitoring tool vhrmo Java 2 02-16-2009 03:53 PM
Very simple swing example from Oracle OracleRenegade Oracle 4 12-01-2008 09:35 AM
Running an RCP application as a simple OSGi bundle bestofmed Eclipse 0 11-28-2008 10:00 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com