The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

Country Detection

  • # April 22, 2013 at 12:21 pm

    I’m finding I get a lot of email and telephone spam from Indian businesses that want me to outsource to them. I don’t want to totally block anyone in India from seeing my site, but since I primarily cater to local businesses, I thought it might make sense to not show my phone number and email address to visitors from India. Can anyone suggest how this might be done using PHP? Most examples I can find through Google rely on third party APIs, which I don’t want to do as it could impact page load times. Is there a database of IPs for each country I could find somewhere and then check each user’s IP against that list?

    # April 22, 2013 at 2:15 pm

    I’m not familiar with IP lookups but you could go for the accepted languages header. Not bulletproof though, just a brainfart:

    $request = getallheaders();
    if (preg_match(‘/hi-IN/’, $request)) { … }

    # April 22, 2013 at 4:12 pm

    Thanks for the idea. Unfortunately, google analytics tells me that they have their language set as english.

    # April 22, 2013 at 4:49 pm

    Looks like I found my solution here:

    They have a simple PHP api and a downloadable database of ip addresses for every city/country. Now to see if this helps cut down the spam, or if they are all hiding behind proxies.

    **Update (almost two weeks later)**: So far so good. Haven’t received any emails (except for via my contact form which is expected) or phone calls from anyone wanting me to outsource in the last couple of weeks. I hope it lasts.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed