Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Displaying users country flags

    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 Code:
    <?php
    if($country == "Scotland") {

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

    }
    ?>
    for every flag, id be here till new year lol

    thanks guys

    Graham

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    If the image's name is exactly same as that of a country's name then you can write the above line dynamically as
    PHP Code:
    $res=mysql_query("select contry from ....") or die(mysql_error());
    $row=mysql_fetch_array($res);
    //..........................
    // 
    Code:
    <img src="flags/<?=$row['country']?>.gif" ........../>
    else make them all to the required format
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder saeed's Avatar
    Join Date
    Oct 2002
    Location
    West Yorkshire
    Posts
    343
    Thanks
    32
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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 Code:
    <?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;
            }
        }

    }
    ?>
    Don't click on this!

    #!/usr/bin/saeed

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate

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

    thanks mate

    Graham


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •