A simple hit counter using PHP

SpaceshipSpaceship
Watch

DarkDevil

Established Member
Impact
0
I'm not going to explain the code, just what to change.

<?
$file=file("counter.txt");
echo ("<font color=#0c4c79 face=arial size=1>[$file[0]]</font>");
$fp=fopen("counter.txt","w");
fwrite($fp,$file[0]+1);
fclose($fp);
?>

The name of the file your counting in, in this case its in the same directory as the page
The colour of the number that will appear on the page
The font type that the numbers on the page will appear in
The size of the text, this is not in pixels. This is in "Microsoft sizes"

To include this into your website use SSI or PHP

SSI- <!--#include virtual="counter.php" -->
PHP- <?php include 'counter.php'; ?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
nice one! this is gonna come in handy

does counter.txt need to be cmoded thoguh? so php can right to it
 
0
•••
I haven't tried it, but I think you would need to chmod the text file to 666 and that should work. B-)
 
0
•••
i thought it would have been :)
 
0
•••
umm.. I was just wondering, wouldn't most people want to display the number of hits after it write to the file? Most of all counters I've ever seen write to the file and then display the number of hits, see what I mean. Ofcourse its not like you can't move that echo to the bottom of the script, i was just asking thats all. btw, Im gonna use that snippet, so thanks!
 
0
•••
Well it all depends.

If you state "X people have visted this website" than it'd be pasttense.. so it makes more sense to exclude The person looking at the site.

If you state "You are the X person to visit this site" than you would ofcorse include that person.

If your simply going to have a number displayed on your website that people will assume is a counter than it doesn't really matter all that much, its all preferance. But counters are never 100% accurate due to people who refresh the page multiple times, yourself visiting the page to test things out, people who share ips with other computers (this would affect peoples attempts at making counters 100% accurate by putting time limits between counts for certain ip's) So whats a count of one gonna change. However if you really wanna be "safe" than create a text file and make the number inside it 1.




on a side note... Who Missed Me?
 
0
•••
I know there's a pleathera of possibilities but

i was just saying ,thats all lol B-)


but i will use it, so thanks
 
0
•••
Originally posted by DarkDevil
on a side note... Who Missed Me?

Not me lol ........... hah jk. Nice to see you back and we'll look forward to seeing you around! :)

And yes I would say that like zoroko said you'll usually find that most little hit counter scripts display the results after the files have been written to. But I suppose it doesn't really matter either way. :D
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Catchy
CatchDoms
DomainEasy — Live Options
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back