SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using php to check location of user for ad switching?

    I've got a little php script setup to randomly switch between three ads. One of these ads is global but the other two are for amazon.com and amazon.co.uk and I was thinking it might be better to ensure that amazon.co.uk only gets shown to visitors from the UK. Is this a doable check in php? What I have right now is this:

    Code:
    <?php
    
    $ad[] = 
    'AD 1 - Global'
    ;
    
    $ad[] = 
    'AD 2 - Amazon.com - Should be Global except UK'
    ;
    
    $ad[] = 
    'AD 3 - Amazon.co.uk - Should be UK only'
    ;
    
    srand ((double) microtime() * 9999999);
    $random_number = rand(0,count($ad)-1);
    
    echo ($ad[$random_number]);
    
    ?>
    I'd imagine the first step would be to randomize the selection between AD 1 and AD 2 + AD 3. And if it hits AD 2 + AD 3, make a selection based on location.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Here's a start for ya

    http://php.net/manual/en/book.geoip.php

    Code PHP:
    if (geoip_continent_code_by_name($userIP) == "GB") {
    // randomly select from UK ads
    } else {
    // randomly select from non UK ads
    }

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well, I did not know that was in pecl, had always installed maxminds' ext myself. Thanks.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I will see where I can get with that. Handy function.


Bookmarks

Posting Permissions

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