programmingsite : code , tutorials and scripts
Navigation

home


ASP
ASP.net
PHP
JavaScript
C
C++
Java
Visual Basic


Resources
code
tutorials
training

Ads
Products

 

 

Description : A simple text counter on your site





A common feature on most websites is a counter to show how many visitors have been to your site. This is our first example to show how to achieve this

This example is very basic and far from ideal if you want an accurate count of your visitors because if you press refresh your counter will always increment by 1. This example does show some useful techniques such as opening and closing files .

Lets go through the process one step at a time . Firstly you have to create a simple text file and enter the number you wish the counter to start at , note this does not have to be 1 it can be any number you wish.
T he next task will be to upload this to your server , if you have a Unix server this is where you have to watch because you have to set the permissions for your text file so the script can read and write to the file .
To change this you CHMOD your text file to 755. Refer to the help file or documentation of your FTP program to see how to achieve this.


Now for the script , call this something like counter.php

<?php
//this is our text file if you create a different named file change the script below to reflect this
$counter_file = ("counter.txt");
//now we open the file
$visits = file($counter_file);
//this increments the counter value by 1
$visits[0]++;
//now we will open the counter file for writing "w"
$fp = fopen($counter_file , "w");
//put the new count value into the counter file
fputs($fp , "$visits[0]");
//close the file
fclose($fp);
//display the count
echo "There have been $visits[0] visitors so far";
?>

Now add the following to your page where you wish the counter to appear

<?php include("counter.php"); ?>



In Action :

  There have been 17989 visitors so far





Products
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals
Asp Studio 2005
Xtra PHP
Online PHP Script Encoder
Comparison

Links
Make money selling software
Golf resource site
UK price comparison
UK shopping
free articles
free stuff
Gambling resources
Software directory
Health and fitness
Sexy free wallpapers
Hosting resources
ASP site
Beginners PHP
Anime videos
Domain names
Dating site
Programming resources
Dropshippers
Maxi directory
free recipes
Coupons and deals
iPod resources

Ads