2

how can I check if the country is Australia? I need to set a session if country is Australia.

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

dora
  • 21
  • 1

3 Answers3

1

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • is there a very simple way to detect if 1 country only.. thanks for your answer.. – dora Sep 01 '11 at 16:26
  • How is this database not simple? Do you have a question about it? There is example code on their site. – Brad Sep 01 '11 at 16:27
0

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
}
Community
  • 1
  • 1
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
0

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.

steveo225
  • 11,394
  • 16
  • 62
  • 114