Page 1 of 1

Creating an image hit counter Rate Topic: ***** 1 Votes

#1 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Posted 24 January 2009 - 10:15 AM

There are alot of sites on the internet that will allow you to place an image hit counter on your page. The biggest drawback to using these services is they require you to keep ad links in place in excange for using their code.
I myself do not want other sites ads to appear on my pages (if there are ads, they will be mine). To accomplish this, I wrote my own bit of code to do the same thing. It is a suprisingly simple thing to do. Since I did recieve assistance from the very helpful people on this site. I decided to post this tutorial to show everyone how simple it really is.

The most time consuming part of this is creating the images, you can use any image format you prefer, but I like the .gif because of transparancy. The easiest way I have found to create the images is to make a new image with all the numbers on one line:

Posted Image

And then splitting them into individual images:

Posted Image

Name the images with the corresponding number value, for example the image above would be 1.gif and place them in a image folder in the same directory as the following files.

Next, the code:

You need to create 3 files, again all in the same directory:

counter.txt - This file will store the number of hits to the page
phphitcounter.php - Will contain the function to process and display the image
index.php - is a simple example of how to call the function. This will actually be done on the page you want counted.

counter.txt - to start at 0, enter 0 in counter.txt (or you can start with another number)
0



Next is phphitcounter.php - this does it all....
<?php
function displayHits()
{
	  $filename = "counter.txt"; 
	  $openCounter = fopen ($filename, "r"); 
	  $hitCount = fread ($openCounter, filesize($filename)); 
	  fclose ($openCounter); 
	  $hitCount=$hitCount+1; 
	  
	  $updateCounter = fopen ($filename, "w"); 
	  fwrite ($updateCounter,$hitCount); 
	  fclose ($updateCounter);   

	  $hitCount = sprintf("%06d", $hitCount);		  
				
			for($i=0; $i<strlen($hitCount); $i++) 
				{		  
					echo "<img src='images/$hitCount[$i].gif'>";	 
				}
	  
}

?>



and finally, index.php - or place the function call on the page you want counted
<?php 
include("phphitcounter.php");
echo "<center>Number of visits to this page: ";
displayHits();

?>




Breakdown of the displayhits() function:
$filename = "counter.txt"; 
	  $openCounter = fopen ($filename, "r"); 
	  $hitCount = fread ($openCounter, filesize($filename)); 
	  fclose ($openCounter); 
	  $hitCount=$hitCount+1; 



$openCounter opens counter.txt in read mode
$hitCount is then set to the value of the number in the file
counter.txt is then closed
$hitCount is then incremented by 1

$updateCounter = fopen ($filename, "w"); 
	  fwrite ($updateCounter,$hitCount); 
	  fclose ($updateCounter); 



$updateCounter opens counter.txt in write mode
the text is overwitten with the new $hitCount value
counter.txt is closed

$hitCount = sprintf("%06d", $hitCount);		  
				 
			for($i=0; $i<strlen($hitCount); $i++) 
				{		  
					echo "<img src='images/$hitCount[$i].gif'>";	 
				}



$hitcount is formatted as a 6 digit number with 0s leading the count
the loop steps through all numbers in the formatted string, and replaces them with the corresponding image.

I hope that this tutorial was helpful, and I thank those that helped me with formatting the output string with the leading 0s

Is This A Good Question/Topic? 0
  • +

Replies To: Creating an image hit counter

#2 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 16 February 2009 - 10:31 PM

Hi there, you don't know how long i have searched the net for something like this :)

Got a problem though

followed instructions perfectly (very well explained thankyou)
and i directly copied the code - so no mistakes

But in the index.html i get

:blink: Number of visits to this page: "; displayHits(); ?> :blink:


i am sure this is a simple matter of the program not recognizing displayHits()


other things i am unsure of
i created the php (never used it before) - in windows notepad - and then changed the name from .txt to .php - potential problem

called the number 0 - 0.gif (both are zero - but he started from 1 - so unsure)

also, there is no code put into the head of index.html - just the body - once again, unsure.....

lots of possabilities, but i am sure it is a simple mistake

can someone check the code???? - Thankyou

if you want to look at my testpage - although i don't think it would help

http://home.ozonline...ar/zzztestpage/

hey, you can add attachments - cool
- just incase you want to check
Attached File  phphitcounter.php (628bytes)
Number of downloads: 167Attached File  index.html (1.55K)
Number of downloads: 212Attached File  counter.txt (1bytes)
Number of downloads: 180
Was This Post Helpful? 0
  • +
  • -

#3 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 17 February 2009 - 02:27 AM

anyone have any idea's ??????
Was This Post Helpful? 0
  • +
  • -

#4 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Posted 17 February 2009 - 02:30 AM

You need to make the file index.php not index.html....

If you are using PHP in the file, why would you name it .HTML ??? Anyways... that's your problem right there.
Was This Post Helpful? 0
  • +
  • -

#5 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 17 February 2009 - 03:03 AM

tried renaming the index file to index.php

gave me exactly the same result - and besides that, other posts in this forum imbed there php into there html and it works for them????

anyway, tried it - doesn't work
Was This Post Helpful? 0
  • +
  • -

#6 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Posted 17 February 2009 - 03:07 AM

ok... Sure you aren't lying to me when you say you tried renaming it?

http://home.ozonline...tpage/index.php

^ 404, file not found.

http://home.ozonline...page/index.html

^ Shows the error you are experiencing.




The file NEEDS TO BE .PHP to be able to HANDLE PHP SCRIPT. If you don't rename the file to index.php, it will never work. Period.
Was This Post Helpful? 0
  • +
  • -

#7 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 17 February 2009 - 03:31 AM

okay, what i did was rename it to index.php (in the windows explorer rename option) - it didn't work,,,, well actually it worked exactly the same as the html (with the fault) - so i changed it back....

okay, just changed it and deleted the html

but suprisingly, i now get error messages :crazy: :crazy: :crazy:
why didn't this happen before?????

okay, maybe these error messages will give more of a clue....


my apologies, but i did try it before - honest

it is index.php now

sorry pr4y
Was This Post Helpful? 0
  • +
  • -

#8 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 17 February 2009 - 03:38 AM

had a closer look now - wanted to apologize to pr4y - so posted quickly

it seems to have picked up it's own path (which is natural and good) - but i don't recognise the path at all...

it must be the right path for my server????? because all the files are in the same location - with the index.php

except for allocating the path - i'm at a loss :blink:

on my computer, when i test it (index.php) it comes up with my first error message - that i first posted

This post has been edited by nazar: 17 February 2009 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Posted 17 February 2009 - 09:58 AM

Just looking at the errors you are recieving, it looks like you do not have the proper permissions on the webserver to allow for this script to read and write to the counter.txt file. I do not remember how to set these permissions on a linux system, but it looks like that should solve the problem
Was This Post Helpful? 0
  • +
  • -

#10 nazar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-February 09

Posted 17 February 2009 - 05:28 PM

moderator/administrator please delete all messages after #1 (when i started asking questions)

here is a summery of all the problems i had (i had never used php before - so programmers - :D try not to laugh..... :)

:^: firstly, when you save your index.html file (do it to local disk - and then upload it) - before you upload it, change the name to index.php (use windows explorer - right click - rename)
it will work just as well as the html, but now the php code will work!!!!!

:^: second, to open and save a php - open notepad (copy code) - file - save as - use the correct name but change the extension to .php (you wouldn't believe how hard it is to find this info - everyone knows about it, so no one tells you about it :P

:^: third, if you get a permission denied error - when you look at the page - contact your service provider (unless you know how to change it) give them the path and tell them that "counter.txt" needs permission to open, read and write.

:^: fourth, if you get broken pictures for the counter - check the path the images are in (counter images) ... they should be put into a subdirectory called "images" my images directory was called "image" :blink: did you notice the difference :crazy:

:^: lastly, but not least (as hopefully the previous messages will be deleted)
i wish to personally thank Pr4y and d.buckner

Pr4y corrected me on an error

d.buckner - thankyou so much, i have spent days looking/trying code - to get a counter up and running........
as the same as you, i didn't want any external links and especially ads.........
your program is brilliant, i can use any gif images to display the numbers (although yours are great)
and it works exactly how i want it to

thanks mate

thumbs up to this post :^: :^: :^: :^: :^:

This post has been edited by nazar: 17 February 2009 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 kOel2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 09

Posted 26 February 2009 - 12:42 PM

gr8 tutorial ... I`ve been looking for smth like this ...

Thx :^: :^: :^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1