How to create an easy IP based banner rotator?

Discussion in 'Programming' started by luigi777, Jan 23, 2010.

  1. #1
    I need a banner rotator that also is able to show certain ads for US traffic only and other ads for the rest. I've been reading about this and I read that there is a big database needed,... but I just want to make a difference between US and non-US traffic. So basically this is what is needed:

    Check IP ->US -> rotate between these banners
    -> Non-Us -> rotate between these banners

    Any Idea how this can be done or where I can find a script for this?
    THanks
     
    luigi777, Jan 23, 2010 IP
  2. Mr.AD

    Mr.AD Member

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

    I have a idea for you.

    1. Search on google some website contained IP data, try some keywork: ip locator, ip location,... Choose a site that have a simplest layout

    2. Using your language (PHP, ASP, ASP.NET,...) try to get dat you need.

    3. Make banner rotation..

    :)
     
    Mr.AD, Jan 26, 2010 IP
  3. baiwan

    baiwan Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I too am trying to create a banner rotator that will show US visitors (about 50% of total visitors) one set of ads, and others a different set of ads.

    My process so far is as follows:

    download the free ip to country database from http://software77.net/geo-ip/ It's quite big - about 103,000 rows at present.

    import into a MySql database on the server (remove all the comments, then use csv import in phpmyadmin. Make sure to tick "Ignore Duplicates".

    Now you can use something like the following code to check the ip:

    
    $query="SELECT * FROM ip2country2 WHERE ($ip <= ipto) & ($ip >= ipfrom) LIMIT 1";
    if(!($result = mysql_query($query,$PFDB))){report(mysql_error());return false;}
    $x=(mysql_fetch_assoc($result));
    $country=$x['country'];
    
    Code (markup):
    So far so good! I have all this working fine.

    Now I can switch the banner with a bit more simple PHP:

    However, as I have multiple websites (3) I would like to have the banner code etc. on a seperate server, and then in Javascript have code on the remote server select and supply the appropriate banner for that site and user location. I also want to count clicks on the supplied ads to monitor CTR for different banners.

    If anyone can fill in some of the gaps (mostly in the Javascript), I'd appreciate it :)
     
    baiwan, Jan 27, 2010 IP
  4. Mr.AD

    Mr.AD Member

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    41
    #4
    That a great information I found ! Thanks alot ! :)
     
    Mr.AD, Jan 28, 2010 IP
  5. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Depands on the language in which you would like to do it.

    But all of them php, asp etc.. support verification of requesting user system localization. When you get somebody that has localizatioon en-US then you will serve one banner if any other the other banners.

    Using IP for geolocalization is bad idea because you need access to webservice/other service that will check this in databases what usually takes long and it will slow down your website and moreover it is not accurate in many situations.
     
    lovingwings36, Feb 19, 2010 IP
  6. luigi777

    luigi777 Active Member

    Messages:
    429
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Interesting, do you know where to find such scripts?
     
    luigi777, Feb 19, 2010 IP
  7. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7

    For example in asp.net you can use:

    HttpRequest Request = HttpContext.Current.Request;
    string Lang = Request.UserLanguages[0];

    In lang you will have for example en-US, pl-PL, en-UK etc...
     
    lovingwings36, Feb 19, 2010 IP
  8. lovingwings36

    lovingwings36 Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    OpenX supports IP based rotations. It might be worth looking at that ?
     
    lovingwings36, Feb 22, 2010 IP
  9. baiwan

    baiwan Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I've implemented an IP based system with Mysql/PHP, and it works fine - it seems quick enough to me.

    After that, selecting the ads based on geo-location is easy.

    I don't know .asp so I can't comment on that.
     
    baiwan, Feb 22, 2010 IP
  10. baiwan

    baiwan Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I've had a PM'd request for my code for geo-location; rather then just PM it, I thought I'd post it here;

    Goto "http://software77.net/geo-ip" and download the latest IP to Country database.

    Create a database table called ip2country;

    CREATE TABLE IF NOT EXISTS `ip2country` (
      `ipfrom` int(10) unsigned NOT NULL,
      `ipto` int(10) unsigned NOT NULL,
      `registry` varchar(8) NOT NULL,
      `assigned` int(10) unsigned NOT NULL,
      `cc2` char(2) NOT NULL,
      `cc3` char(3) NOT NULL,
      `country` varchar(50) NOT NULL,
      PRIMARY KEY  (`ipfrom`),
      KEY `ipto` (`ipto`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Code (markup):
    Then use csv import in phpmyadmin to import the data. Make sure to tick "Ignore Duplicates".

    Now you should have a table of data of around 100k plus rows.

    I placed the PHP in 2 functions - see below;

    function db_get_country_from_ip($ip){
    //is it quartet (192.166.2.2) or INT
    if(strpos($ip,'.',1)){if(($ip=db_quart2int($ip)) == false){return false;}}
    $query="SELECT * FROM ip2country WHERE ($ip <= ipto) & ($ip >= ipfrom) LIMIT 1";
    $x=db_do_row_query($query);
    return $x;
    }
    
    function db_quart2int($ip){
    $parts=explode('.',$ip,4);if(count($parts) != 4){return false;} //malformed ip
    return $parts[3]+($parts[2]*256)+($parts[1]*65536)+($parts[0]*16777216);
    }
    Code (markup):
    I use a third function for database access;

    function db_do_row_query($query){
    if(!($result=mysql_query($query,PFDB))){report(mysql_error()); return reporte($ex=debug_backtrace());}
    if((mysql_num_rows($result) == false)){return false; }
    //-------- Fetch the data 
    if(!($t=mysql_fetch_assoc($result))){return false;}
    return $t;
    }
    
    Code (markup):
    you can easily run the query directly however.

    The first function "db_get_country_from_ip" uses the "db_quart2int" function to convert from a quartet (e.g. 127.0.0.1) to an integer number which can be used with the database.

    It then creates a query to run on the database, which in this case is performed by "db_do_row_query($query)"

    The return value is false if no match is found, or else an array of the database row. (It will operate slightly faster if you re-write it with `cc2` in place of the * in the query, but I prefered to have all the data in the return value).

    Then, early in your code put something like this:

    global $User_Country;
    $country=db_get_country_from_ip($_SERVER['REMOTE_ADDR']);
    $User_Country=$country['cc2'];
    
    Code (markup):
    Now wherever you want to do something geo-location specific, just test the value of $User_Country. (The values are quite normal - "us" for the USA, "uk" for the UK and so on)

    Easy huh!

    (Btw - on my server the whole thing takes around 9ms (0.09 seconds)

    Good luck with it :)
     
    baiwan, Feb 23, 2010 IP
    MrCalc likes this.
  11. FavouritesBlog

    FavouritesBlog Peon

    Messages:
    846
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Can I ask, do you already have a site script for banner exchange or are you looking for a simple script which can display specific banners to your USA visitors on your site?

    I may be able to find you already available in opensource.
     
    FavouritesBlog, Feb 23, 2010 IP
  12. xInd

    xInd Notable Member

    Messages:
    2,025
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #12
    I think the key is that we need to make the script handle the visitors on other websites. The banner will be distributed to networks of sites, and when it's displayed on those sites wherever they are, it needs to display the correct banner according to where that particular visitor is. I'm sure this is done before, but if we can't find what you're looking for we will figure out putting a custom script together for you.
     
    xInd, Feb 23, 2010 IP