Deny access from entire country with php

I’m thinking I could one of $_server variables to grab what country they are coming and then write a redirect script that sends them to a offline page or some other website.

Do you I have the correct approach here?

There is no server variable that tells you the country a visitor is located in. You should look into an IP-to-location database (which you will have to update regularly as IP allocations frequently change). MaxMind’s GeoLite is free –

hmm, i thought one of those things like HTTP_USER_AGENT outputted the language and country?

I know in Coldfusion, when i output this I see en_US in the user agent info.

However my site is in PHP, so i don’t have Coldfusion available but I would assume that PHP would have similar variable.

What i want to do is block all of Ecuador from seeing my website so i grabbed that database of ip addresses you sent me.

Man they should make this earier to do.


That is the locale which is just a user setting, it doesn’t have to (and often doesn’t) correspond to a location. Just what language the user wants to use the OS in. And it’s not always sent to you either.

You can have the COUNTRY server variable if you install mod_geoip for apache. But then you don’t even need php to deny access to whole coutry. With mod_geoip you can just add this to you apache config:

This is just a sample of what you would have in httpd.conf

GeoIPEnable On
GeoIPDBFile /usr/local/src/GeoLiteCity.dat
GeoIPOutput Env

Flag all requests from China with BlockCountry var


And now you have the BlockCountry enviroment variable.
You can use it anywhere like this, in this example you would deny the forum posting from any country flagged as BlockCountry

<FilesMatch “newreply\.php|newthread\.php”>
Deny from env=BlockCountry

Of you can just deny all requests like this:

<Directory />
Deny from env=BlockCountry

I’m hosted with MediaTemple, do you know if they can enable this mod_geoip param?

I think i’d rather control it at server level then write a PHP script and clutter my pages up with scripts.

You do realize GeoIP solutions are trivially defeated with Proxies, right?

What do you have against Ecuador?

I have nothing against Ecuador. I love all of my Ecuadorians. This was a client request. :slight_smile:

I think the GeoIP solution will work – I’m gonna check with MediaTemple to see if they can set me up with that.