treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Display Content Based on IP Address

  • Hi everyone! I'm currently interested in displaying a quick line or two of copy if a visiter's IP address falls between a certain range. Do any of you have any resources or ideas of how I can go about it?

    I'd like to use JS for this. I'd imagine I can use JS to detect the IP then use an IF statement to display said content. I'm pretty new to JS and is the reason why I would love some help from ya'll :)

    Any help is MUCH appreciated. Thnx!
  • yeah... all right ok ... it can be possible but there is more than just js for this to work if u want to display content depending on country...etc... but if you know the IP address of the person then is much easier here is a php Snippet to get the job done...
    <?php
    /*
    this Snippet allows you to display content "ONLY" to a visitor from
    a specific IP addresses in a page.
    Change the $allowed "value" to the IP address you want.
    */
    $allowed = '00.000.000.000';
    $userip = $_SERVER['REMOTE_ADDR'];
    if($userip == $allowed){
    echo "This content is only viewed by the IP address you chose...";
    }
    ?>


  • Thanks man, not exactly what I was looking for, but it's good to know the php side of things.

    I'm really trying to do this with JS. I'm also trying to do a range of IP addresses.

    Thanks for your input!
  • As far as I know JavaScript can't detect IP address's, it is client side remember?

  • var ALERT_TITLE = "Oops!";
    var ALERT_BUTTON_TEXT = "Ok";
    var remoteadd = '<?php echo $_SERVER["REMOTE_ADDR"];?>'
    var targetIpAdd = "00.00.00.00"
    window.onload=function(){
    if(remoteadd==targetIpAdd) alert("info here");
    }
  • You can find the IP address and the location details using ip-details.com.
    It gives your current ip address and location details with the location
    map with latitude, longitude coordinates.
  • use the javascript service provided by geolify.com