PDA

View Full Version : [TUT] Super Simple Page Hits Counter



HabSound.com
13-11-2008, 02:52 AM
Hey!
This is just to show some newbies to coding how to make a Hits Counter.

1. Create a File called hits.dat
2. Copy this code and read the comments to understand it!




<?
//See's if you made hits.dat!
if(file_exists("hits.dat"))
{
//This part shows that you did!
$exist_file = fopen("hits.dat", "r");
$new_count = fgets($exist_file, 255);
//Adds a New Hit
$new_count++;
//closes the file
fclose($exist_file);
//shows the new hit count
print("$new_count");
$exist_count = fopen("hits.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count);
}
else
{
//You Didnt Make a Hits.dat File!
echo("Please make a File named Hits.dat!");
?>


It Doesnt Detect by IP or anything. JUst shows how many times the Page has been viewed.

Pretty Bad Ey!

Meti
13-11-2008, 09:35 AM
I am not good at all with PHP, so I dont know if this is a good code or not, but nice of you releasing it!
Let`s wait and see what PHP coders say ^+^

Invent
13-11-2008, 10:56 AM
Just a few changes I'd make...



<?php

// Check if the file exists or if we can write to the folder..
if( file_exists( 'hits.dat' ) || is_writeable( './' ) )
{

// Open the file or if it doesn't exist, make it..
$exist_file = fopen( 'hits.dat', 'a+' );

while( !feof( $exist_file ) )
{

// Store the contents of the file in the variable $new_count..
$new_count = fgets( $exist_file, 500 );

}

// Close the file handle...
fclose( $exist_file );

// Check if the variable $new_count has a value, if not, make it '0'
$new_count = ( $new_count ) ? $new_count : '0';

//Adds a New Hit
$new_count++;

// Echo the hit(s)..
echo $new_count;

// Make a new handle for the file, this time we truncate the file to 0 length so we can add the new hits..
$exist_file = fopen( 'hits.dat', 'w+' );

// Write the new amount of hits to the file..
fwrite( $exist_file, $new_count );

// Finally, we close the handle to the file to save memory space and to avoid corruption of the file..
fclose( $exist_file );

}
else
{

// Alert the user..
echo 'Please make the file hits.dat';

}

?>

Calon
13-11-2008, 12:36 PM
Nice too see that you're releasing it for free, a tip; don't use short tags :(

Hypertext
13-11-2008, 01:59 PM
I'll implement uniques in a bit, off to school, I'll do in study hall or whatever.

HabSound.com
14-11-2008, 01:38 AM
Just a few changes I'd make...



<?php

// Check if the file exists or if we can write to the folder..
if( file_exists( 'hits.dat' ) || is_writeable( './' ) )
{

// Open the file or if it doesn't exist, make it..
$exist_file = fopen( 'hits.dat', 'a+' );

while( !feof( $exist_file ) )
{

// Store the contents of the file in the variable $new_count..
$new_count = fgets( $exist_file, 500 );

}

// Close the file handle...
fclose( $exist_file );

// Check if the variable $new_count has a value, if not, make it '0'
$new_count = ( $new_count ) ? $new_count : '0';

//Adds a New Hit
$new_count++;

// Echo the hit(s)..
echo $new_count;

// Make a new handle for the file, this time we truncate the file to 0 length so we can add the new hits..
$exist_file = fopen( 'hits.dat', 'w+' );

// Write the new amount of hits to the file..
fwrite( $exist_file, $new_count );

// Finally, we close the handle to the file to save memory space and to avoid corruption of the file..
fclose( $exist_file );

}
else
{

// Alert the user..
echo 'Please make the file hits.dat';

}

?>



That Makes it Better

Blinger1
15-11-2008, 06:43 AM
This seems good :)! I might use it one day... lol

Want to hide these adverts? Register an account for free!