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

<?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-badge.tar.gz (210) , extract in the same folder. and you’ve got your own badge now.

Share

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

Comments (11)
  1. Great post … I would like to display

    Country
    Region
    Language
    OS
    Browser
    Proxy

    By using ipblocks – Zone files last updated: Fri Jul 1 15:07:40 EDT 2011
    http://www.ipdeny.com/ipblocks/data/countries/
    http://www.ipdeny.com/ipblocks/

  2. Good post …. I would like to display

    Country
    Region
    Language
    OS
    Browser
    Proxy

    By using ipblocks – Zone files last updated: Fri Jul 1 15:07:40 EDT 2011
    http://www.ipdeny.com/ipblocks/data/countries/
    http://www.ipdeny.com/ipblocks/

  3. 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

(required)

(required)


*

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=""> <strike> <strong>