开发者

Need to set session if country is australia

how can I check if the country is Australia? I need to set a session if country is Austral开发者_如何转开发ia.

207.209.7.0   | 207.209.7.255 

This is the range IP of australia.

How can I use $_SERVER['REMOTE_ADDR'] in that part without database, just a simple if statement


First off, you shoud know that this isn't guaranteed. People can get around it, data will never be 100% complete, etc.

Simply checking a block of IPs is not acceptable. You need to use a geolcation database, such as MaxMind's GeoIP. The free database is good enough for country-level geolocation.


Check out this question about how to detect someone's location in php. Then add your condition.

Since you don't want to use a database I would recommend

$doc->loadXML(file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=your_key&ip=" . $_SERVER['REMOTE_ADDR'] . "&timezone=false")); 

$country = $doc->getElementsByTagName('CountryName')->item(0)->nodeValue;

if ($country == 'australia') {

    // set session
}


If you absolutely know the ranges of valid IP Addresses, you could do something like:

function ip2number($IP) {
    $parts = explode('.', $IP);
    if(count($parts) != 4) return 0;
    return ($parts[3] + $parts[2] * 256 + $parts[1] * 256 * 256 + $parts[0] * 256 * 256 * 256);
}

$num = ip2number($_SERVER['REMOTE_ADDR']);
if($num >= ip2number('207.209.7.0') && $num <= ip2number('207.209.7.255')) {
    echo "Valid";
}

Like Brad says though, it isn't guaranteed and there are ways around it, and you will have to keep up to date of the valid IP Address ranges.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜