Pin Me

HTML Code for Counters

written by: S. R. Obbayi•edited by: Amy Carson•updated: 10/8/2010

Counters are a great way to keep track of the number of visitors to your website or anything else for that matter. Want to take a peek into some HTML code for counters? Look no further as this tutorial will let you understand how counters on websites are created.

  • slide 1 of 4

    Introduction

    To implement some useful HTML code for counters, you are going to have to find a way to store data used to determine the counter value. Luckily this is not a problem when it comes to websites. You can always rely on a database to do this for you.

    Unfortunately by the very nature of HTML, it is not possible to store persistent data within the confines of HTML. Therefore the HTML will need some help preferably from a server-side language like PHP, JSP, ASP.NET, RUBY and any others out there.

    In this article I will show you how to save the persistent data for the counter index in a database using PHP as our server-side code to complement the HTML.

  • slide 2 of 4

    The HTML Source Code for the Counter

    HTML Counters The counter can be in the form of text or in the form of an image. The image on the left shows both samples. Both styled to look the same. Seeing it is possible to style the counter to look similar to the one with the image, it makes more sense to just go with the text based version as dynamic images have overheads to overcome that are beyond the scope of this tutorial as compared to simple text.

    The source code you would need to attach either of these counters to your page is:

    <div style="border: 1px solid #ff0000; height: 30px;width: 165px; font: bold 12px verdana;color: #ff0000; background: #000000;text-align:center">You are visitor number <br /><?php get_count();?></div>

    The code above uses a text-based solution while the code below uses a more complicated image based solution which I have included just for your knowledge, but we will not need to implement it.

    <div style="border: 1px solid #ff0000; height: 30px;width: 165px; font: bold 12px verdana; color: #ff0000; background: #000000;text-align:center">You are visitor number <br /><img src="path/to/counter/script.php" height="15" border="0" /></div>

  • slide 3 of 4

    The Server Side Source Code For The Counter

    The server-side code needs to determine is if the visitor is unique. In this case it can use cookies or IP addresses for detecting that the user has previously visited the website. If it is a hit counter then of course this does not apply as hits can count repeat visitors.

    This code assumes that we are going to use the IP address to determine who has previously visited and who has not. Take note that using IP addresses for determining who is unique is not a full proof method and the details are beyond the scope of this article.

    Also assumed is that you have already got a database set up on your web server with a table holding the IP addresses which is constrained to hold unique items.

    The code starts off by retrieving the IP address using the built-in PHP variable $_SERVER['REMOTE_ADDR'] as follows:

    function get_count(){$ip_address = $_SERVER['REMOTE_ADDR'];mysql_query(“INSERT INTO ip_addresses (ip) values ('$ip_address')");$result = mysql_query(“select COUNT(*) from ip_addresses");return mysql_result($result, 0); }

    The code above returns the correct result whether the insert fails or not. If it fails it is because the particular IP address is already in the database, if it succeeds well and good. Either way the figure it returns after counting is what is expected as we want to display total users ever visited up to that point in time.

    The get_count() function above would be placed somewhere before the code where it is used in the HTML file wrapped in these <?php … ?> tags. If you were to use cookies instead of IP addresses then you would use the built in PHP variable $_COOKIE['name_of_cookie'] instead of $_SERVER['REMOTE_ADDR'].

  • slide 4 of 4

    Conclusion

    If you were counting hits instead of visitors you would not need to save the IP addresses. You would need a table with a single row and an integer value of zero, which is incremented by one every time a user visits the page.

    Follow these tips on creating a site map for a website to learn more about web development. You can also have a look at this article on other resources for free web design elements.






© Copyright 2016 brighthub.com.