Hello!
I've been searching alot after a simple tutorial that shows how to make a unique counter using a .txt file, but none of them was good/working.
So now i ask you fellows, do you know any good & working tut?
PHP Counter using .txt file
Started by Chrypetex, Oct 28 2006 11:54 AM
6 replies to this topic
#1
Posted 28 October 2006 - 11:54 AM
#2
Posted 28 October 2006 - 01:21 PM
Here it is, just wrote it up
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Unique Hits Counter ( Using flat files )</title>
</head>
<body>
<?php
// File where we will store the counter data
$counterFile = 'counter.txt';
// The user's IP
$userIP = getenv( 'REMOTE_ADDR' );
// Read the file into an array ( every line of the file will be a piece of the array )
$contentArray = file( $counterFile );
foreach( $contentArray as $lineNum=>$lineContent )
{
// Split each line by |
list( $IP, $visits ) = explode( '|', $lineContent );
// Make sure the visits is recorded as an integer
$uniques[ $IP ] = (int) $visits;
}
if( $uniques[ $userIP ] > 0 )
{
// If it's a returning visitor, increase the # of visits from their IP
$uniques[ $userIP ]++;
}
else
{
// If it is somebody's first visit, add them to the list and give them 1 visit
$uniques[ $userIP ] = 1;
}
//=================
// Update our counter file
//=================
foreach ( $uniques as $ip=>$visits )
{
$newContent .= "$ip|$visits \n";
}
if( $handle = fopen( $counterFile, w ) )
{
if( !fwrite( $handle, $newContent ) )
{
echo 'Error updating counter file!';
}
}
else
{
echo 'Error opening counter file';
}
//=================
// Final # of unique visits.
// If there is only 1 unique visitor, we don't want the "s" at the end of visitors
//=================
$totalUniques = count( $uniques );
$visitorsEnd= ( $totalUniques == 1 ) ? false : 's';
echo 'You have visited this site ' . $uniques[ $userIP ] . ' times <br />';
echo "We have had $totalUniques unique visitor{$visitorsEnd}.";
?>
</body>
</html>
I threw in a little proper grammer. Just make sure that the counter.txt file is writable
Edited by cheerio, 28 October 2006 - 01:35 PM.
#3
Posted 28 October 2006 - 01:28 PM
That would be really nice!
Looking forward to that!
#4
Posted 28 October 2006 - 05:51 PM
K Post Updated
#5
Posted 29 October 2006 - 05:55 AM
Thanks you! That was really nice of you!
Will try it right now!
Will try it right now!
#6
Posted 18 December 2006 - 05:22 PM
Awsum stats script, easy to use, displays Total Hits, Unique Hits, Todays Hits, & Todays Unique Hits!
+ Download.
view tutorial here
+ Download.
view tutorial here
#7
Posted 18 December 2006 - 06:24 PM
Braunson, on Dec 18 2006, 05:22 PM, said:
Awsum stats script, easy to use, displays Total Hits, Unique Hits, Todays Hits, & Todays Unique Hits!
+ Download.
view tutorial here
+ Download.
view tutorial here
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
