IP Based Image

Discussion in 'PHP' started by oo7ml, Dec 27, 2012.

  1. #1
    Hi, i have a banner area on my website where i would like to display an image. I would like to display a different image in the banner area, depending on a users' location.

    I have 4 different images:

    01 - usa_banner.jpg (default)
    02 - europe_banner.jpg
    03 - uk_banner.jpg
    04 - australia_banner.jpg

    If a user views the site from a country outside of the top 4 areas, then i would like to display the default usa_banner.jpg image.

    I have created my site using codeigniter. What is the best way to work out the IP ranges etc. Any advice on this would be greatly appreciated. Thanks in advance...
     
    oo7ml, Dec 27, 2012 IP
  2. sandip.dalvi

    sandip.dalvi Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,


    You can use below code.


    <?php
    // PHP file.
    $ip = $_SERVER['REMOTE_ADDR'];

    $country = exec("whois $ip | grep -i country"); // Run a local whois and get the result back
    $country = strtolower($country); // Make all text lower case so we can use str_replace happily
    // Clean up the results as some whois results come back with odd results, this should cater for most issues
    $country = str_replace("country:", "", "$country");
    $country = str_replace("Country:", "", "$country");
    $country = str_replace("Country :", "", "$country");
    $country = str_replace("country :", "", "$country");
    $country = str_replace("network:country-code:", "", "$country");
    $country = str_replace("network:Country-Code:", "", "$country");
    $country = str_replace("Network:Country-Code:", "", "$country");
    $country = str_replace("network:eek:rganization-", "", "$country");
    $country = str_replace("network:eek:rganization-usa", "us", "$country");
    $country = str_replace("network:country-code;i:us", "us", "$country");
    $country = str_replace("eu#countryisreallysomewhereinafricanregion", "af", "$country");
    $country = str_replace("", "", "$country");
    $country = str_replace("countryunderunadministration", "", "$country");
    $country = str_replace(" ", "", "$country");
    ?>
     
    Last edited: Dec 27, 2012
    sandip.dalvi, Dec 27, 2012 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Cool, thanks... would you be better of using a 3rd party service for this.
     
    oo7ml, Dec 27, 2012 IP
  4. casiopeia

    casiopeia Guest

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    there are some location api services in web that can do this with more precision. I dont know if i can give links, but google is your friend :D
     
    casiopeia, Dec 27, 2012 IP
  5. ogah

    ogah Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    for 3rd paty service you can googling with query "ip to location"
     
    ogah, Dec 27, 2012 IP
  6. sandip.dalvi

    sandip.dalvi Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can use the 3rd party API. If API down then this will not work.
     
    sandip.dalvi, Dec 27, 2012 IP
  7. MS.USD

    MS.USD Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php

    $ip = $_SERVER['REMOTE_ADDR'];
    $api_key = "fa21adbc3ead7eda75b0192aa6b296e7ddba9a6e71c32018ae1938289241af58";
    $data = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key='".$api_key."'&ip=74.125.45.100&format=json");
    $country_code = json_decode($data);
    $code = $country_code->{'countryCode'};

    echo '<img src="'.$code.'.jpg"/>';


    ?>
     
    MS.USD, Jan 3, 2013 IP
  8. junedc

    junedc Active Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    you're sharing your api key is this okay ?
     
    junedc, Jan 9, 2013 IP
  9. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #9
    Has anyone ever used MaxMind?
     
    oo7ml, Jan 15, 2013 IP
  10. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #10
    I use the maxmind geolite. Good stuff you can have your server cron a monthly update of the free database and then use it locally on your server. it just becomes a call into your own framework to get a country code.
     
    goliath, Jan 15, 2013 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #11
    Uhm, ever heard of str_ireplace?

    Just saying. Much less you convert it to lower-case, then run upper case compares?!? Also, you don know you can used arrays for that, right? And what the devil is with the double quotes around the $country var for nothing?!?

    
    <?php
    $ip = $_SERVER['REMOTE_ADDR'];
    $country = exec("whois $ip  | grep -i country"); // Run a local whois and get the result back
    $country = str_ireplace(
    	array(
    		'country:',
    		'network:country-code:',
    		'network:organization-',
    		'network:organization-usa',
    		'network:country-code;i:us',
    		'eu#countryisreallysomewhereinafricanregion',
    		'countryunderunadministration',
    		' '
    	),
    	array(
    		'','','',us','us','af',''
    	),
    	$country
    );
    
    Code (markup):

    Should be functionally identical. NOT that I would use exec to do such a thing -- since that wouldn't be cross platform and may be blocked on some hosts. (both the exec and the whois)

    ... and since grep is doing the extraction, wouldn't just detecting : on the line be faster?

    I'll have to play with this :D
     
    deathshadow, Jan 16, 2013 IP