Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with blocking content from specific IP's

    So basically I need some help of where to start with this code. I need to write a script that will block content on a web page if someone is accessing it from a IP from specific countries.

    More specifically I need to show certain products on my webpage if the consumer is accessing the page from Canada. I don't really know where to start but I imagine it might be something like this?

    f IP address in Canada
    {
    show product 1
    HTML code....
    else
    Show product 2
    HTML code....
    }

    I prefer to use HTML & Java script
    otherwise if anything else can be combined with HTML page it is fine with me

    Any help would be awesome!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Not sure if you can do this in JavaScript; but even if you could, you'd be better off using a server-side solution (ie, ASP, PHP, JSP, ColdFusion, etc.)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,886
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    You will need an IP to location database or equivalent.

    The database is really way too large to convert into something usable by JavaScript, so you almost surely would need to use server-side code for this.

    Also, these databases are not free. Only a few hundred dollars per year, depending on what features you want, but not something a hobbyist would normally use.

    There are also so RESTFUL services out there that you could use, but I don't know of any that are free for more than a handful of queries per day.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,886
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Just for example, the ip2location database that we use has over 8 million records in it and is updated with from 50,000 to 100,000 changes each month.

    The current count of records just for Canada is just under 150,000.

    No practical way to put that much data into client-side code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    if all you need is the user's country name, you can use a free IP lookup like maxmind:

    Code:
    <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
    <script type="text/javascript"> 
    alert("you are in "+geoip_country_name())
    </script>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,886
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Ahh...that's even simpler than the RESTFUL url that I had in mind.

    Are there no "terms of service" though, for using that? Oh, never mind...I looked it up.
    http://www.maxmind.com/en/javascript

    So staring May 1st, it's free if you give them attribution. But pretty darned cheap even if you don't.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    That answers a question which has been asked many times. But of course anyone who really wants to access the site can simply disable Javascript. So you should place the whole thing in a <div> with style="display:none", and then use Javascript to change it to style.display="block" for users from authorised countries. Note that makes it harder to allow access for all countries except (say) Canada.

    I have noticed a practical difficulty in identifying the country. Is the USA returned as US, USA, United States or what? The UK is returned as "United Kingdom". Is the country name returned with its English spelling or its proper name in the local language? Finland vs Suomen Tasavalta, etc. How can you find out unless you are in Finland? It would help if a list of the countries was shown!!

    Perhaps you have to subscribe to find out!
    Last edited by Philip M; 03-23-2013 at 10:09 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Hello Philip,

    you're right - there's alot of room for error in using the country name. Safer would be to use geoip_country_code(), which returns "a two-character ISO 3166-1 country code":
    http://en.wikipedia.org/wiki/ISO_3166-1

    http://dev.maxmind.com/geoip/javascript

  • Users who have thanked xelawho for this post:

    Philip M (03-23-2013)


  •  

    Posting Permissions

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