...

View Full Version : Displaying users country flags



graham23s
12-25-2007, 02:46 PM
Hi Guys,

i have a folder full of flags for nion each country, i store the users country in mysql as the country name i.e "Scotland", i was going to display the corresponding flag next to the users profile is there an easier way than:



<?php
if($country == "Scotland") {

echo "<img src="flags/scotland.gif">";

}
?>


for every flag, id be here till new year lol

thanks guys

Graham

abduraooft
12-25-2007, 03:41 PM
If the image's name is exactly same as that of a country's name then you can write the above line dynamically as


$res=mysql_query("select contry from ....") or die(mysql_error());
$row=mysql_fetch_array($res);
//..........................
//


<img src="flags/<?=$row['country']?>.gif" ........../>
else make them all to the required format :)

saeed
12-26-2007, 09:21 AM
I dont know whats wrong with this script. When I'm using static IP in example.php its working fine but doesn't giving me results using REMOTE_ADDR function. Please help me in this.

example.php

<?php

require_once('CountryFromIP.inc.php');

//$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$hostname = $_SERVER['REMOTE_ADDR'];
$ip = $hostname;
//$ip ='202.83.168.107';

$object = new CountryFromIP();

$countryName = $object->GetCountryName($ip);
$flagPath = $object->ReturnFlagPath();

echo "Your computer IP address is ". $ip .", suggests that you are reaching this page from <i>". $countryName. " </i> <img src=".$flagPath." border='0'>";
?>

CountryFromIP.inc.php

<?Php
/**
* This class generates the country name and its flag from its IP address
*
*
* @author Rochak Chauhan
*/

class CountryFromIP {

private $CountryIPDatabase = 'CountryIPDatabase.txt';
private $ip = '';

/**
* Function to validate IP ( please modify it according to your needs)
*
* @param $ip - string
*
* @return boolean
*/
public function ValdateIP($ip) {
$ipArray = explode(',',$ip);

if(count($ipArray) != 4) {
echo "<font color='red' size='3'> <b>ERROR: </b> Invalid IP</font>";
return false;
}
else {
return true;
}
}

/**
* Function to return Country name from the IPDatabase
*
* @param $ip string
*
* @return string - name of the country, false otherwise
*/
public function GetCountryName($ip) {
$this->ip = $ip;
$ip = sprintf("%u", ip2long($ip));

$csvArray = file($this->CountryIPDatabase);

for($i=0; $i<count($csvArray); $i++) {
$arrayOfLine = explode(',', $csvArray[$i]);
if($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
return $countryName = $arrayOfLine[2];
}
}
return false;
}

/**
* Function to return local path to Country's flag
*
* @param $ip - string
*
* @return string - local path to flag image
*/
public function ReturnFlagPath() {

if($countryName = trim(ucwords(strtolower($this->GetCountryName($this->ip))) )) {
$countryName = str_replace(' ','%20',$countryName);
return "flag/$countryName.gif";
}
else {
return false;
}
}

}
?>

graham23s
12-26-2007, 03:06 PM
Hi Mate

yeah that would have been a much better solution lol staring me in the face:)

thanks mate

Graham



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum