School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,360 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,703 people online right now. Registration is fast and FREE... Join Now!




Creating an image hit counter

 
Reply to this topicStart new topic

> Creating an image hit counter

Rating  5
d.buckner
Group Icon



post 24 Jan, 2009 - 09:15 AM
Post #1


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:

IPB Image

And then splitting them into individual images:

IPB 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)
CODE

0


Next is phphitcounter.php - this does it all....
CODE

<?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
CODE

<?php
include("phphitcounter.php");
echo "<center>Number of visits to this page: ";
displayHits();

?>



Breakdown of the displayhits() function:
CODE

$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

CODE

$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

CODE

$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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

nazar
*



post 16 Feb, 2009 - 09:31 PM
Post #2
Hi there, you don't know how long i have searched the net for something like this smile.gif

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.gif Number of visits to this page: "; displayHits(); ?> blink.gif


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.com.au/nazar/zzztestpage/

hey, you can add attachments - cool
- just incase you want to check
Attached File  phphitcounter.php ( 628bytes ) Number of downloads: 26
Attached File  index.html ( 1.55k ) Number of downloads: 29
Attached File  counter.txt ( 1bytes ) Number of downloads: 28
Go to the top of the page
+Quote Post

nazar
*



post 17 Feb, 2009 - 01:27 AM
Post #3
anyone have any idea's ??????
Go to the top of the page
+Quote Post

pr4y
Group Icon



post 17 Feb, 2009 - 01:30 AM
Post #4
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.
Go to the top of the page
+Quote Post

nazar
*



post 17 Feb, 2009 - 02:03 AM
Post #5
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
Go to the top of the page
+Quote Post

pr4y
Group Icon



post 17 Feb, 2009 - 02:07 AM
Post #6
ok... Sure you aren't lying to me when you say you tried renaming it?

http://home.ozonline.com.au/nazar/zzztestpage/index.php

^ 404, file not found.

http://home.ozonline.com.au/nazar/zzztestpage/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.
Go to the top of the page
+Quote Post

nazar
*



post 17 Feb, 2009 - 02:31 AM
Post #7
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.gif crazy.gif crazy.gif
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
Go to the top of the page
+Quote Post

nazar
*



post 17 Feb, 2009 - 02:38 AM
Post #8
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.gif

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 Feb, 2009 - 02:46 AM
Go to the top of the page
+Quote Post

d.buckner
Group Icon



post 17 Feb, 2009 - 08:58 AM
Post #9
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
Go to the top of the page
+Quote Post

nazar
*



post 17 Feb, 2009 - 04:28 PM
Post #10
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 - biggrin.gif try not to laugh..... smile.gif

icon_up.gif 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!!!!!

icon_up.gif 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 tongue.gif

icon_up.gif 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.

icon_up.gif 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.gif did you notice the difference crazy.gif

icon_up.gif 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 icon_up.gif icon_up.gif icon_up.gif icon_up.gif icon_up.gif

This post has been edited by nazar: 17 Feb, 2009 - 10:59 PM
Go to the top of the page
+Quote Post

kOel2
*



post 26 Feb, 2009 - 11:42 AM
Post #11
gr8 tutorial ... I`ve been looking for smth like this ...

Thx icon_up.gif icon_up.gif icon_up.gif icon_up.gif icon_up.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 07:50PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month