How i can do this ?

Discussion in 'PHP' started by coun_vincent, May 20, 2011.

  1. #1
    Hello ,

    i've some banner ads ( banner1.png , banner2.png , banner3.png )

    i need to show banner1 for US & CA traffic and banner2 for UK and banner3 for rest of the world traffic .

    Please how i can do that in php ? can anyone help me with the code ?

    thank
     
    coun_vincent, May 20, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    function countryCityFromIP($ipAddr)
    {
    //function to find country and city from IP address
    //Developed by Roshan Bhattarai http://roshanbh.com.np
    //verify the IP address for the
    ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
    $ipDetail=array(); //initialize a blank array
    //get the XML result from hostip.info
    $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
    //get the city name inside the node <gml:name> and </gml:name>
    preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
    //assing the city name to the array
    $ipDetail['city']=$match[2];
    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
    //assign the country name to the $ipDetail array
    $ipDetail['country']=$matches[1];
    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
    $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
    //return the array containing city, country and country code
    return $ipDetail;
    }
    PHP:
    Source
     
    ssmm987, May 20, 2011 IP
  3. mobileappdoctor

    mobileappdoctor Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For this you would use GEO-Targeting. There are tons of good examples out there, unfortunately I cannot post any links yet. First step is getting a GEO database, I would recommend GEOLITE City from maxmind. Same company has a PHP API for using these databases and then it should be very simple switch statement where you want to show a specific banner if you were trying to add the code directly into a page.

    Another option would be to use Open X which has a Geo-Targeting plug-in.
     
    mobileappdoctor, May 20, 2011 IP
  4. coun_vincent

    coun_vincent Well-Known Member

    Messages:
    803
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    130
    #4
    thanks for fast reply , i've installed the GEOIP on my dedicated server and configured it and it's working well after some small code for test .

    i will try ssmm987 code and also i will googling on any other code , still need more replys and info to make good things

    thanks
     
    coun_vincent, May 20, 2011 IP