I'm going to state my 'thing' and hopefully somebody can help me, or at least give me a kick for the start.
I'm building 1 webpage for 3 different public ( one in the US, in the UK and New Zealand ). The thing is that page will be almost the same, with some minor ( or major ) content changes.
What do I need... Is a PHP script ( working with a cookie ) which automatically catches the visitors IP and sends him to his region ( so if you're from US and you enter the UK address you will be redirected automatically to the US site ). Beside this, I would like to add an exception. Like a special page, which only I'm aware off, where you will get an cookie that allows you to visit any page even if you're from NZ and you want to visit the US page.
Giant thanks if someone can help me with this ( I'm not quite 'in-love' with cookies and I'm slightly new for PHP as well :P )
This is the variable which is contain visitor's IP
Code:
$_SERVER[REMOTE_ADDR];
what you need is making a list of IP from UK, US, example, UK's IPs start with 911.911
Code:
if (preg_match('/^(911\.911\.)/' , $_SERVER[REMOTE_ADDR]))
{
header('location: site/uk.php');
}
If user from UK want to visit US's page, i have an idea, is that the re-director code will only execute ONE TIME per visitor, so next time user click on "US Page" link they wont be redirected again.
the only thing that still needs to be done is the think when an UK IP wants to get to the US page. I would really like that to be automatic and the cookie thing sounds quite nice ...
But mate, thank you soooo much for your help ( now I have a start, at least )
I would use a "splash" screen on the first visit to allow the user to select the region they want. One instance where automatically checking for them may not be a good idea is when someone is on vacation in another region or if their proxy server is located in another region but they may physically be somewhere else. I would then store their selection in a cookie but still provide a way to re-select the region if needed (for example if someone moves or wishes to access another region for any reason).
@criterion9
I know what you mean, but the thing is that the US are not allowed to see the UK due to copyright restrictions, the UK can't see the NZ for the same reason and so on. So visitors need to stick on their region
That's why I would like to make this automatic, without splash page ( that was my first idea, but it's not the right choice in this case ).
I understand about copyright...you just have to realize that ip-redirection is not perfect and there could be many cases where the redirection will not go to the correct region. What happens when an American is traveling through the UK?
@criterion9
if he's going to use local internet ( UK ISP ), he will see the UK page. He is allowed to see the UK if he's on British land ( and the same thing goes for each country )
Here is another hypothetical...for example in China right now many people are viewing the internet through proxy servers in other regions. Your auto-detect script would display the wrong region for those users.
@criterion9
Those whom use proxy ( Chinese, American, British, etc ) can view any of the pages, that's their problem. My hands are tied because of that stupid copyright and stupid requirements.
Ah and btw... the rest of the world ( beside US and NZ ) are going to see the UK page
Makes sense. I just wanted to make sure you were aware of the potential pitfalls with automatic detection. There are many geo2ip services if you do a quick search. I can't think of any that are free though (I also didn't search very hard either).
@criterion9
Oh thanks for all the help mate. I was sure of my decision, that's why I posted this thread. I'm going to take a look to the geo2ip services and hopefully something will be found!
No IP address starts with 911. IP addresses are 4 bytes long, and a byte can only store 0-255.
@OP, if you want to determine location from IP address your best bet is to use a database of IP addresses to counties. I've used this one in the past with some success.
You can also improve the detection of real IP sometimes by tracing back through proxies. Obviously doesn't help for transparent proxies but it might be a bit more accurate over all:
PHP Code:
function get_ip() { switch (false) { case empty($_SERVER['HTTP_CLIENT_IP']): return $_SERVER['HTTP_CLIENT_IP']; case empty($_SERVER['HTTP_X_FORWARDED_FOR']): return $_SERVER['HTTP_X_FORWARDED_FOR']; default: return $_SERVER['REMOTE_ADDR']; } }
The thing above with the 911 was only an example, I got the point .
I do really want to thank you for the list. I think I'm going to use that one, if you say that the thing works okay. I'm worried that much about proxies, because the real target of the web page doesn't include people whom use proxies .
Bookmarks