Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Web page counter

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

bomber
Junior Member
*


Posts: 34
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 250 (Donate)
Post: #1

Wink Web page counter


A simple application and one you can put in any page. For example you could drop a single include() line in any PHP page you like to show a hit-counter for, or tell your web server to include this script at the bottom of every page (IIS and Apache at least can do this).

This is a text counter, not one with images..

PHP Code:
<?
  
/* Start of the script. If there's a counter file, read it and get the current
   * number of hits from it. Then we add one, display the number, and rewrite the 
   * file. */

  
if ( is_file("counter.txt") ) {

    
/* The file exists, so read it. The file() function reads a whole file
     * into memory. With a counter file, which is tiny, there's no problem 
     * with this. The file in memory is stored as an array with one line 
     * per array item, so array[0] is the first line, etc. We only need the 
     * first line */

    
$file file("counter.txt");

    
$count rtrim($file[0]); 

    
/* So $count now contains the first line, minus any whitespace and/or 
     * newlines at the end. This means $count should be just a number. 
     * Lucky for us, PHP's clever enough to turn useless text into 0 as a 
     * number, so we can just add 1 to $count and save that, whatever was
     * in there before! */

    
$count++;
  } else {
    
// There was no counter file, so we'll start from scratch.
    
$count 1;
  }

  
/* Now write the count back to a file, and display the number */

  /* First, open the file to write .. */

  
if ($file_pointer fopen("counter.txt""w") ) {

     
/* Write the counter to the file we just opened */     

     
fwrite($file_pointer$count);

     
/* Close the file */
     
fclose($file_pointer);

  } else {

     
/* $file_pointer wasn't set, so fopen() failed, 
      * so we can't save the counter. Say so! */
 
     
echo "Couldn't save counter!";

  }

  
// Finally, show the counter.
  
echo "This page has been visited $count times";
?>

18-09-2006 11:36 PM
Find all posts by this user Quote this message in a reply
clookid
Advanced Member
***


Posts: 184
Group: Registered
Joined: Oct 2006
Status: Offline
Reputation: 4
Points: 134 (Donate)
Post: #2

RE: Web page counter


Awesome, I always thought that it would be really hard to make a page counter in PHP, but it isn't!


Veebra Articles - Computer and technology related articles.
08-10-2006 08:58 AM
Find all posts by this user Quote this message in a reply
addik
Member
**


Posts: 109
Group: Registered
Joined: Oct 2006
Status: Offline
Reputation: 2
Points: 0 (Donate)
Post: #3

RE: Web page counter


clookid Wrote:
Awesome, I always thought that it would be really hard to make a page counter in PHP, but it isn't!


Yeah, I had the same thoughts. I have always wonder how they do those page counter. but since I am learning PHP, I quite understand its ability now.

08-10-2006 04:03 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  change every page in the directory to same page? bobis 2 147 20-11-2008 07:44 AM
Last Post: shadav
  How to add attachment feature in members reply page? WoRLDLiFE 18 645 27-10-2008 09:48 PM
Last Post: sohom
  Need simple vist counter script sevestar 1 239 18-02-2008 03:58 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 02-12-2008, 10:02 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB