How To Create a “LIVE” Visitor Counter | Minervity
How to Create an Illustrated Cartoon Character
82,224 Views | 42 Comments

So, I decided to attempt to create a tutorial on how to create a unique illustrated cartoon character in Photoshop. It’s pretty straight forward and the skills you need for this is very limited as …

Read the full story »
Creative Corner

A series of in depth and heavily inspiring Graphic, Web and Logo Designer and Developer interviews.

Designer Apps

A resource vault of applications that would serve any Designer or Developer well to have in their arsenal.

Freelance Hub

A storage full of tips and tricks for every freelance Designer or Developer that wants to progress their business.

Geek Things

Every once in a while we’ll post a new geeky thing that might inspire you to go through the day with a laugh or a second thought.

Website Filter

A collection of websites that is sure to bring you creative inspiration and keep you on the edge of design.

Home » Flash

How To Create a “LIVE” Visitor Counter

Submitted by Richard Darell20,832 Views | 41 Comments

Create A Live Visitor Counter - Article Title ImageIntroduction

In this tutorial I will teach you how to create a “LIVE” visitor counter that actually updates itself. “LIVE” means that the counter, made in flash, checks for new visitors every 20 seconds or so (this you will be able to set to whatever interval you think is best suited for your needs). A working example of this can be found on top of this website along with in the end of this tutorial.

The execution of this tutorial is very straight forward and doesn’t really demand any particular skills in either flash, photoshop, FTP or PHP other than the basic understanding of the three. There are however some important tasks that needs to be done in order for it to work properly. But I will guide you through the process best I can to ease your reading.

Creating this Live Visitor Counter I will be using the following software’s and Languages:

  • Adobe Flash
  • Photoshop
  • PHP Scripting
  • FTP Client

Alright that should be it. Let’s start off this tutorial!

Step 1

The first thing you need to do is to create a background for your counter (this is however optional and is just for effect). I decided to make a slightly bigger counter than the one I have on top of this site. The tutorial one measures 200*38 and has a red background.

What you would want to think about when creating this background is to give it some depth (again for effect). Doing this gives the counter a slighter more advanced look and more appealing to look at as well. The trick here is to create a second or more layers with for ex. a “shine” layer which you later put on top of your flash application in it’s own layer. This creates the illusion that there actually is some glass above the text itself.

Here is the final background in all it’s simplicity. As you can see there is a “shine” layer on it as well.

Counter Background

Make sure you separate the two layers in Photoshop making them a JPG “background” file and a PNG “shine” file. For an in depth tutorial on how to create the Shiny Button look check my earlier tutorial: “Make A Shiny Button

Step 2

Now when we have our background we can go ahead and do the necessary assembly in Flash. Open up Flash and open up the “Document Properties” panel. Set the dimensions to 200 (width) and 38 (height). Also change the frame rate from 12 to 10 to give it an exact count in milliseconds. You can also change the background color to black (#000000) to make it easier to see on a white background if something is not aligned properly. Before clicking OK you can go ahead and change the Title of the project to “Live Visitor Counter. Click Ok.

Document Properties

Step 3

Now, create four (4) new layers making the total five. Name them (from the bottom up) “background”, “visitor counter”, “labels”, “shine” and “actions”.

Layer Setup

Step 4

Now to some ActionScript. Make sure your “Publishing Settings” are set to use ActioScript 2.0 as this is how this is read. OK now, in the first frame of the “actions” layer open the “Actions” edit view and type:

this.loadVariables(“counter.php?num=”+random(999));

This will call for our PHP script that will handle the actual page view counting. We will deal with the PHP script after we’re done assembling our Flash movie.

Step 5

Create a second frame in the “actions” layer and open up the “Actions” edit view again. Type in:

this.loadVariables(“count.txt?num=”+random(999));

This will load the actual number of visitors from the holder file “count.txt” after our PHP script has done it’s job.

Now your layers should look something like this.

Actions Layout View

Step 6

Now, let us go ahead and add the background that’s going to become our actual visitor counter. Import you background JPG in the first frame of the “background” layer. Make sure you align it to the top left corner so it covers the entire work area.

Also go ahead and add the shine to the counter by importing the shine PNG to the first frame of your “shine” layer. Don’t forget to align it just like you did with the background layer. To the top left corner. Your layout should now look something like this.

Counter Graphics - Assembled

You can also go ahead locking these to layers to be sure nothing is moved ad throws off your alignment.

Step 7

Time to add some labels to make sure the visitor/viewer knows what this application is counting. In the first frame of the “labels” layer add these two labels to your counter application. “LIVE VISITOR COUNTER” in the top right corner and also “NUMBER OF VISITORS” in the bottom right corner. I have chosen to use the font “ARIAL” size 8 for the top label and “ARIAL” size 7 for the bottom one in order to be able to make a distinction of the two.

Label Layout

Step 8

Here come the part where we actually incorporate the number count for our counter. It’s quite simple but there is some things you have to pay close attention to. In the “visitor counter” layer add a zero (0). Make the alignment of the text area “Align Right” to make sure the counter increases from right to left and nothing else. I have also chosen a bit grayer color for the number count to make the effect of the “shine” layer more eye pleasing. One important task here is not to forget to drag the text area all the way to the right to make sure that when the counter increases in muber of characters it doesn’t clip in the middle of the application. You should en up with something like this:

Number Alignment & Layout

Step 9

With the number count text field still selected edit “count” in it’s VAR field in the text fields “Properties”. It’s case sensitive so make sure you edit “count” in only lower case character. What’s really important as well is to make sure you text field is “Dynamic Text” and not “Static Text” or “Input Text”. The counter simply won’t work if you forget this step.

Text Field Properties

Step 10

To make the entire application to update itself I have chosen the easiest way to accomplish this. When we chose 10 frames per second in the beginning of the tutorial we actually made sure that when we create more key frames the run time of the application is increased with one second per 10 frames. This makes it easy for us to choose whatever time interval we want the application to have. Let’s go ahead and add frames all the way up to 200 to make the application to update itself every 20 second.

You accomplish this easiest by going to the 200th frame, selecting it and pressing F5 for each layer and you have created yourself frames all the way up to 200.

Application Interval

As you can see I have locked all the layers to prevent any movement within the layers of the elements.

Step 11

We need the application to return to the first after running it’s entire cycle. Therefor, select the 200th frame of the “actions” layer and press F6. This will create a sole key frame at the 200th frame. With the frame still selected open up the Actions Edit view and insert the following code:

gotoAndPlay(2);

This will make the application to return to frame 2 of the flash movie. Now you might wonder “Why number 2?”. Well, the first frame increases the number by one every time it’s executed. If we were to return to the number 1 frame of the flash movie it would mean that every time it started over it would increase the counter number by 1. In this case we would get an application that increased it’s number every 20 seconds. Returning to number two ensures that we only increase it ones and after that we just pick up the current number of visitors. You should now have something like this:

Loop Actions Layout

Step 12

Now go ahead and save your flash movie. After saving it go to “Publish Settings…” and make sure you’ve set your ActioScript to 2.0. Also, for best quality make sure you have the “BEST” quality selected and also the highest JPG quality. Here’s how my settings look:

Publish Settings

Go right ahead now and publish you flash movie by selecting “Publish” from the “File” menu. You can also press Shift+F12 to accomplish this. Make the name of your movie “Live Visitor Counter”.

Step 13

OK. We have out flash movie visitor counter application done. Now it’s time to look in to the PHP scrips need to make all this work. Open your choice of text editor. I prefer “Notes”  or “Type Pad” as it is a pretty easy word processor. With it open type this:

<?php
$count = file_get_contents(“count.txt”);
$count = explode(“=”, $count);
$count[1] = $count[1]+1;
$file = fopen(“count.txt”, “w+”);
fwrite($file, “count=”.$count[1]);
fclose($file);
print “count=”.$count[1];
?>

This script is the core of the counter so to speak. I will try and explain it as simply  as I can so you know what it does. But before doing this please save this as “counter.php” in the same directory as you flash movie.

Line 1:

<?php

Opens up the php scripting channel declaring that this will be a php script.

Line 2:

$count = file_get_contents(“count.txt”);

This line loads the content of our (to be created) count file which is the current number of visitors before incresing it.

Line 3:

$count = explode(“=”, $count);

This line breaks down the count.txt file and just fetches the actual number of visitors.

Line 4:

$count[1] = $count[1]+1;

This line increase the number of visitors by 1.

Line 5:

$file = fopen(“count.txt”, “w+”);

This line “opens” the file for us to be able to write the new number back into the file.

Line 6:

fwrite($file, “count=”.$count[1]);

This line writes our new number into the file.

Line 7:

fclose($file);

This line closes the file. If we don’t close the file there will be a memory leakage further down the road as our script simply will open a new file again leaving the old still open.

Line 8:

print “count=”.$count[1];

This line pushes the new number of visitors to our variable “count” in our flash movie simply updating it.

Line 9:

?>

This line simply close our script telling the computer the script has now ended.

Step 14

What we need to now is to create the actual file that holds the visitor number. This file will be loaded and increased every time someone enters your site (depending on where you put this flash movie of course). Open up your favorite text editor again and type in the following:

count=0

Save your file as “count.txt” in the same directory as your newly created flash file and your PHP script.

Step 15

Now we are done with all the background work. Now it’s time to put it in to your website. Upload “Live Visitor Counter.swf“, “counter.php” and “count.txt” to your root directory on your server. We need to do this as the counter executes it’s stuff from the root rather then a directory making the script a bit more complicated.

Step 16

Alright. To incorporate you newly created flash counter on your website/blog. Simply put this code wherever you feel you want it to be located on the site.

<object classid=”clsid:d27cdb6e-ae6d-11cf-96b8-444553540000″ codebase=”http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0″ width=”200″ height=”38″ id=”Live Visitor Counter” align=”middle”>

<param name=”allowScriptAccess” value=”sameDomain” />

<param name=”allowFullScreen” value=”false” />

<param name=”movie” value=”Live Visitor Counter.swf” />

<param name=”quality” value=”best” />

<param name=”bgcolor” value=”#000000″ />

<embed src=”Live Visitor Counter.swf” quality=”best” bgcolor=”#000000″ width=”200″ height=”38″ name=”Live Visitor Counter” align=”middle” allowScriptAccess=”sameDomain” allowFullScreen=”false” type=”application/x-shockwave-flash” pluginspage=”http://www.macromedia.com/go/getflashplayer” />
</object>

That should be pretty much it. Now test your new live visitor counter to make sure all is in good order. Here is the finished counter as an example:

Round Up

Now when everything should be working you could simply sit and watch your counter and it will increase whitout reloading the page. Try open another browser and reload the page there. Quickly jump back to this page as you’ll sit it increase in a few seconds. Pretty cool huh!

Hope you have enjoyed this tutorial and that you will make good use of this technique. It can get quite mesmarizing at times to sit and watch it so don’t get addicted to it. LOL

If you have any additional question please don’t hesitate to ask. More tutorials coming up soon.

It’s been a pleasure creating this tutorial for you and I wish to see you back again and again!

Thank you!

Popularity: 15% [?]

41 Comments »

  • Rob said:

    Awesome work! In the intro, you should mention that a working example can be seen on the top of this web page.

    [Reply]

  • Twinster (author) said:

    Rob – Thanks man! Oh, thought I did. Haha. Will edit it and push it in. Thanks for pointing that out!

    [Reply]

  • CSQRL said:

    why have “count=” in the count.txt file if all you’re doing is parsing around it, ignoring that text completely, and updating the number?

    (edit:removed formatting & sample code)


    $count = (int)trim(file_get_contents('count.txt'))++;
    @file_put_contents('count.txt', $count);
    echo "count=$count";

    [Reply]

  • Twinster (author) said:

    CSQRL – Interesting point.However Flash needs that to gather the number from the file. The counter updates itself without you having to update the page. echo:ing the text file result will only update the counter everytime you reload the page if you’re only using PHP. The flash movie does this in order for it to update automatically.

    That is if I haven’t misunderstood your editing entirely. :)

    [Reply]

  • Minervity » Online Applications - Week 7 - “Free Is King - Freemium” said:

    [...] Spotlight » How To Create a “LIVE” Visitor Counter Thu, 29/01/09 – 10:26 | 4 Comments [...]

  • Minervity » How To Create a “LIVE” Count-Up said:

    [...] Spotlight » How To Create a “LIVE” Visitor Counter Thu, 29/01/09 – 10:26 | 5 Comments [...]

  • Chris Risse said:

    Excellent tutorial. I will keep this in mind in the future.

    [Reply]

  • Twinster (author) said:

    Chris Risse – Thank you very much! Glad you liked it!

    [Reply]

  • Minervity » How To Create a Flash XML Slideshow Player said:

    [...] will enhance the feel of your website for your every visitor. Everything from Count-Up’s and Live Visitor Counter’s to Expansive XML Music [...]

  • Kalvis said:

    but every time when page is refreshed it count like visitor

    [Reply]

  • Minervity » Advertisement - Week 10 - “Hard Work Pays Off - First Ad Campaign Received” said:

    [...] site is up at almost 19.000 displays right now and counting ever 20 secs (yes I am using the “Live Counter” which I have created a tutorial for. It’s a great motivator and a great way to add [...]

  • JDStraughan said:

    Killer tutorial! Added to tutlist.com

    [Reply]

  • JDL said:

    Hi

    If the counter does not count and I’ve gone over the code at length, what might be the problem?

    thanks

    [Reply]

  • Twinster (author) said:

    Kelvis – Yes, there are several things you can do to set the counter to only count unique visitors. I will see if I can add an addition to this to enable that when I get more time.

    JDStraughan – Thanks m8! Awesome! Glad you liked it!

    JDL – Well, it could be the version of Flash you’re using but also the flash player you have installed. Make sure you have the latest versions. Also, When debugging my script, which is copied exactly as is, everything is wroking just fine. I have yet to figure out why this ” is such a messup for some versions of flash.

    [Reply]

  • Erik said:

    Nice one Twinster [ two thumbs up ]

    @Kelvis if you wish not to increment the counter every time the browser is refreshed, use the php sessions to check if the counter has been incremented or not just like this:

    session_start();

    if( !isset( $_SESSION['count'] ) )
    {
    $_SESSION['count'] = TRUE;

    $count = file_get_contents(‘count.txt’);
    $count = explode(‘=’, $count);
    $count[1] = $count[1]+1;
    $file = fopen(‘count.txt’, ‘w+’);
    fwrite($file, “count=”.$count[1]);
    fclose($file);
    print “count=”.$count[1];
    }

    For sure the counter will not increase not unless the browser cookies is removed and the session expires.

    [Reply]

  • Paula said:

    Thanks for the counter. It really works well.
    I just wanted to double check – if someone leaves the pages will the number on the counter also drop?
    Thanks

    [Reply]

  • faje said:

    Hi twinster, cool stuff lovely tutorial.. Anywayz, I’m trying to create a flight schedule using flash and Xml but i’m kind of stuck.. any solutions?

    [Reply]

  • Twinster (author) said:

    Paula – No, it’s a form of a Live pageview/visitor counter updated in realtime so there’s no dropping in numbers.

    Faje – Would help if you told me what you are stuck on.

    [Reply]

  • Paula said:

    Thanks for your quick response.
    So far it works really well.
    I noticed in Flash – the HTML had the loop feature activated. Should it be?
    Is there such a program that adds and drops as visitors come and go? Is it possible that this one could be tweaked to accommodate that?
    Thanks!

    [Reply]

  • Twinster (author) said:

    Paula – Yeah, I am sure it can be tweaked in to performing such IP scanning. However, I haven’t messed around with that much. Will have to have a look at that some day.

    The loop feature should be turned on as it doesn’t matter. There are “stop;” tags around the script but that doesn’t matter really. The script is intended to keep the counter updated every 20 seconds.

    [Reply]

  • Paula said:

    Thanks!

    [Reply]

  • Alex said:

    Thanks for the great tutorial mate, very easy to follow as always :)

    [Reply]

  • Serkan said:

    Good work thank you..

    [Reply]

  • Daily Links | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster? said:

    [...] How To Create a “LIVE” Visitor Counter | Minervity Great look at creating a "Live" visitor counter (tags: tutorials flash coding counter php development programming tips) [...]

  • JT said:

    This is a really cool tutorial, and i’m really digging the design of your site.

    Keep up the good work!

    [Reply]

  • Ramy said:

    tnkss 4 this great tutorial but i have a problem …
    i do every thing like you say but it doesn’t work with me
    her is the file i made with counter.php and count.txt and the final code to incorporate
    http://www.4shared.com/file/104124123/a3d2dedd/MyCounter.html
    and this is my site which i made the counter in it
    http://proangel.2ya.com

    please help me with that

    best regard

    [Reply]

  • venkat said:

    seems to be very difficult to implement for an average blogger.

    [Reply]

  • ryan said:

    unluckily, i have followed the guide step by step to make the counter, but it fails to increase the number after put to the root directory of my server. Twinster, please help me to cope with this~ thx~

    My file is attached below:
    http://www.sendspace.com/file/d9dlj2

    [Reply]

  • Darroll said:

    I like this technique. I was looking to see if you have something similar to a counter that would increase as users paged thru the site, but also decrease as visitor closed or left the site.

    Similar to ‘online users’.

    Any info or directions would be greatly appreciated.
    D

    [Reply]

  • kaise said:

    Hi,
    Can someone please provide me with the photoshop images as I do not have photoshop. i’m a newbie and would reaaly love to try this out.

    anyone who does have and can send it to me please email me on kaise@hotmail.co.uk

    [Reply]

  • Dasher said:

    I came across your site looking how to create a simple counter but this was just a BIG PLUS i like it, now I’d like to jump to the next stage which for me I’d like to know how to resolve IP’s origin, what I mean is that I’d like to know where my visitor coming from what part of the globe, China, Italy, Rome, you know… and have a way to display a little flag of that country … I like this tutorial thank you for sharing it.

    [Reply]

    Danni Reply:

    Hi there, Ive been searching all this out on the net day and night as trying to gathering info to set our own website up and came across this site which does exactly what your asking for http://www.tracemyip.org hope this helps it tells you everything to home town, country, flag browser, os, isp, ip etc. hope it helps and good luck

    [Reply]

  • Sanjay said:

    Hi, i am currently using http://www.histats.com counter, this is good my i don’t like this. Because my website http://www.moneyinhands.com is a small website & i want to create my own counter. Above article is good but i need full tutorial to i will make my own counter without using another website. If you have good examples please reply me soon.

    [Reply]

  • Raymark said:

    ..i tried this tutorial.. but i had a problem cause the counter doesn’t work..i mean it doesn’t increment..i used only macromedia flash not the adobe one..does it matter? i haven’t upload yet my website i only use a wampserver..could you help me in this problem?..
    i will appreciate your help..it’s for our system project in school.

    [Reply]

  • Hero said:

    Hey and thanks for this tutorial

    if you are lazy and use the downloaded version, there’s a bug in the PHP code,

    $file = fopen(“count.txt”, w+”);
    should be like this,

    $file = fopen(“count.txt”, “w+”);

    Make sure you change the permissions Attributes (CHMOD) of the text file to 777

    after this it works

    [Reply]

  • LynetteCook19 said:

    I had a dream to begin my company, but I didn’t have enough of cash to do it. Thank goodness my colleague proposed to use the credit loans. Hence I took the college loan and made real my old dream.

    [Reply]

  • NFLjerseys said:

    One way to buy cheap jerseys is shopping online, NFL jerseys at nfljerseys-shopping.com are sold at a discount, most of football jerseys are having promotion. Why not action now?

    [Reply]

  • Wholesale Gadgets said:

    Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler

    [Reply]

  • Wholesale Cell Phones said:

    Thank you for providing good information,Wholesale Electronics and Wholesale Gadgets from pickegg.com,Pickegg.com is an online Wholesale Electronics store. Offers consumer electronics and electronic gadgets at best price

    [Reply]

  • halil özdemir said:

    Teşekkürler.. :D

    [Reply]

  • LED Key Holder said:

    thanks for your sharing, it is a great post for me.

    [Reply]

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

Anti-Spam Protection by WP-SpamFree

Spam protection by WP Captcha-Free