Show GeoIP Badge On Your Website Using PHP And JavaScript

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.
[cfields]geoipbadge[/cfields]
First thing we have to do is create badge generator, which we will use later. let’s call it geo.php

<?php
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
	$IpAddr=$_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; ?>";

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

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

<html>
<head><title>geoip</title></head>
<body>
<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>
</script>
<script language="JavaScript" src="http://www.example.com/geo.php"></script>
</body>
</html>

You can follow the instructions step by step above, or simply by downloading this geoip-gd-badge.tar.gz (38 downloads ) , extract in the same folder. and you’ve got your own badge now.

11 Comments

    • Josh

      It would be great to have those for identifying proxies

      HTTP_X_FORWARDED_FOR : –
      HTTP_X_FORWARDED : –
      HTTP_FORWARDED_FOR : –
      HTTP_FORWARDED : –
      HTTP_VIA : –

      Also, those ones
      HTTP_X_COMING_FROM : –
      HTTP_COMING_FROM : –

      • not every proxy forward client informations to web server, in fact, not every client using proxy, except for those who intercept by transparent proxies.
        as long php support those predefined variables and the browser send those variables, that would be easy to accomplished.
        —-
        I changed my statement.
        mostly, proxies hide information about their client. so it would be hard to find real client’s ip.even using javascript/Ajax/ActiveX, security does not let us reveal computer’s ip easily.

        there’s a conversation on how to detect LAN ip using java
        http://www.exampledepot.com/egs/java.net/Local.html
        but it need lot of effort.

    • i think that can be done, i’ll take a look and see what i can do
      but this does not require IPdeny country block

  1. steve

    Hi,
    I don’t understand why. I’ve uploaded the files, changed the URLs (“www.example.com/flags/” and “www.example.com/geo.php”) … but it doesn’t work at all.

    PHP is enabled and it’s ok I’m using sometimes, and it works if I use this URL “www.kutukupret.com/geo.php”, it doesn’t if I use “www.mydomain.com/geo.php” (having all the files on “mydomain.com”, of course).

    Is there a fixing ?
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *