Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2008, 12:53 PM   PM User | #1
markjon
New to the CF scene

 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
markjon is an unknown quantity at this point
Simple Hit counter

A hit counter will let us know how many times a page is accessed. In case one visitors loads the page several times, the hit counter will increase several times (but this is likely to happen only a few times).

The code for the hit counter bellow will save the number of hits in a file named counter.txt (the name of this file may be changed). Each time the page is loaded, the file will be read, the number will be increased by one and the new number will be saved to the same file.

<?php

//The file where number of hits will be saved; name may be changed; p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";

// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits = fgets($fp,100);
fclose($fp);

//increading number of hits
$hits++;

//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);

//printing hits; you may remove next line (and keep the counter only for your records)
print $hits;

?>
To use this code, copy it to your page in the exact position where you want to show number of hits.

Last edited by Inigoesdr; 10-13-2008 at 03:12 PM.. Reason: Signature rules are there for a reason
markjon is offline   Reply With Quote
Old 10-13-2008, 02:01 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Who cares about hit counters anymore?

My server logs and web stats provide me with all the info I need.
The users don't care because I can make that number say anything I want it to say.
mlseim is offline   Reply With Quote
Old 10-13-2008, 03:10 PM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by mlseim View Post
Who cares about hit counters anymore?

My server logs and web stats provide me with all the info I need.
The users don't care because I can make that number say anything I want it to say.
Now, now; Don't be jealous you didn't think of it first.
Inigoesdr is offline   Reply With Quote
Old 01-13-2009, 10:57 PM   PM User | #4
pauls74462
New Coder

 
Join Date: Dec 2008
Posts: 36
Thanks: 4
Thanked 0 Times in 0 Posts
pauls74462 is an unknown quantity at this point
clear this up

Quote:
Originally Posted by markjon View Post
$hits++;
Does '$hits++;' and '$hits = $hits + 1;' do the same thing?
pauls74462 is offline   Reply With Quote
Old 01-13-2009, 11:35 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by pauls74462 View Post
Does '$hits++;' and '$hits = $hits + 1;' do the same thing?
Yes..
__________________
ZCE
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:50 AM.


Advertisement
Log in to turn off these ads.