Description
This script will include a simple text counter on the page of your choice. The scripts do not require a database of any kind.
Text Counter in PHP
Just include the following code where you want to include a file in a .php file.
<?
$counter_file = ("counter.txt"); //file where count is stored
$visits = file($counter_file); //puts the count in a variable
$visits[0]++; //add 1 to the counter
$fp = fopen($counter_file , "w"); //open the file for write
fputs($fp , "$visits[0]"); //write the new value to the file
fclose($fp); //close the file
print $visits[0] . " visitors so far.";
?>
Text Counter in ASP
Use the following code to include a counter in your page in ASP. Create the file counter.txt with nothing but the number 0 in it before
you try to use the following script
<%
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("C:\path\to\counter.txt")
oldNumber = CLng(oFile.ReadLine) 'read the current count out of the file
oFile.Close 'close the file because we're done reading it
oldNum = oldNum + 1 'add 1 to the current counter number
Set oFile = oFSO.OpenTextFile("C:\path\to\counter.txt",2,true)
oFile.WriteLine(oldNum) 'write the new counter value to file
oFile.Close 'close the file
Response.Write(oldNum & " visitors so far.")
%>
|