Teen Programmers Unite  
 

 

Return to forum top

page hit counter

Posted by bhavin_777 [send private reply] at September 28, 2001, 06:48:12 PM

hello ,
i want the source code of page hit counter.anyone can give it ti me?
or can i find it anywhre?

Posted by gian [send private reply] at September 28, 2001, 06:59:59 PM

Did you have a specific language in mind? Because usually they have to be server side, so I will write it in PHP because that is how I will...

<?php
$fp = fopen("counterfile", "a");
fwrite($fp, "\n");
fclose($fp);
$fp = fopen("counterfile", "r");
$file = fread($fp, filesize("counterfile"));
$split = explode("\n", $file);
$visits = sizeof($split);
print "You are the $visits visitor to this page";
?>

Thing is PHP, and I'm guessing you were wanting a javascript one, well I've denounced client-side, so bla...

Go to thecounter.com or something.

Posted by Psion [send private reply] at September 28, 2001, 08:05:00 PM

gian is the posterchild of computer science. Way to go with linear time algorithms to check the number of hits so far!

Posted by Setherd123 [send private reply] at September 28, 2001, 10:01:52 PM

You make me throb all over, Psion.

Posted by gian [send private reply] at September 28, 2001, 10:18:48 PM

Fine...
<?php
$a++;
print "$a visits";
?>

There. Only, the PHP interpreter will be invoked each time, and the variables will be initialized again.

Posted by taubz [send private reply] at September 29, 2001, 11:56:31 AM

Just open the counter file, read a number, add one to the number, print out that number, and resave the new number to the file...

- taubz

Posted by Psion [send private reply] at September 29, 2001, 05:21:50 PM

taubz, then you have to make sure locking is all done correctly, for when you have simultaneous hits.

Posted by taubz [send private reply] at September 30, 2001, 10:05:19 AM

Not if your site doesn't get that much traffic.

Going back to gian's code:
$file = fread($fp, filesize("counterfile"));
$split = explode("\n", $file);
$visits = sizeof($split);

What's the point of that? It's just filesize("counterfile"), no? (In which case it's constant time!)

- taubz

Posted by gian [send private reply] at September 30, 2001, 04:15:23 PM

Yes, that would be more efficient... I was probably brain dead, sleepy, or both...
Psion: Split second access times, I doubt it...

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.