Jun 302011

Remember my last article about how to show visitor’s ip address on our website? today, we will modify the script a little bit to do something more interesting, not only displaying visitor’s ip address but also displaying visitor’s country and flag. Unfortunately this script unable displaying the flag and country for ipv6 yet. The badge will look something like this.
First thing we have to do is create badge generator, which we will use later. let’s call it geo.php

if ( isset($_SERVER["REMOTE_ADDR"]) ) {

$cc = geoip_country_code_by_name($IpAddr);
$country = geoip_country_name_by_name($IpAddr);


if (typeof(v_geo_BackColor)=="undefined")
	v_geo_BackColor = "white";
if (typeof(v_geo_ForeColor)=="undefined")
	v_geo_ForeColor= "black";
if (typeof(v_geo_FontPix)=="undefined")
	v_geo_FontPix = "16";
if (typeof(v_geo_DisplayFormat)=="undefined")
	v_geo_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%%%FLAG%% %%COUNTRY%%";
if (typeof(v_geo_DisplayOnPage)=="undefined" || v_geo_DisplayOnPage.toString().toLowerCase()!="no")
	v_geo_DisplayOnPage = "yes";

v_geo_HostIP = "<?php echo $IpAddr; ?>";
v_geo_Country = "<?php echo $country; ?>";

if ( $cc != FALSE ) { ?>
        v_geo_Flag = "<br><img src='http://www.example.com/flags/<?php echo strtolower($cc); ?>.png' />";
} else { ?>
        v_geo_Flag = "";

if (v_geo_DisplayOnPage=="yes") {
	v_geo_DisplayFormat = v_geo_DisplayFormat.replace(/%%IP%%/g, v_geo_HostIP);
	v_geo_DisplayFormat = v_geo_DisplayFormat.replace(/%%FLAG%%/g, v_geo_Flag);
	v_geo_DisplayFormat = v_geo_DisplayFormat.replace(/%%COUNTRY%%/g, v_geo_Country);
	document.write("<table border='0' style='padding: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -khtml-border-radius: 5px; border-radius: 5px; background-color:" + v_geo_BackColor + "; color:" + v_geo_ForeColor + "; font-size:" + v_geo_FontPix + "px'><tr><td>" + v_geo_DisplayFormat + "</td></tr></table>");

We can use these bagde from anywhere on our website using the following code by using a simple inline javascript, or we could try it first with this simple code. let’s call it geo.html

<script language="JavaScript">
v_geo_BackColor = "grey";
v_geo_ForeColor = "white";
v_geo_FontPix = "16";
v_geo_DisplayFormat = "<strong>You are visiting from :</strong><br>IP Address : %%IP%%<br>%%FLAG%% %%COUNTRY%%";
v_geo_DisplayOnPage = "yes";
<script language="JavaScript" src="http://www.example.com/geo.php"></script>

You can follow the instructions step by step above, or simply by downloading this [download#37] , extract in the same folder. and you’ve got your own badge now.

  11 Responses to “Show GeoIP Badge On Your Website Using PHP And JavaScript”

Comments (11)
  1. is your php support for geoip module?

 Leave a Reply

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




This site uses Akismet to reduce spam. Learn how your comment data is processed.