Search code examples
phpgeocountrycountry-codes

PHP - Detect country


I want to detect my visitor's country.

And I have found the solution at Google, but the result is nothing.

Can you help me?


Solution

  •     <?php 
    // Author: www.easyjquery.com 
    $ip = $_SERVER['REMOTE_ADDR']; 
    // remember chmod 0777 for folder 'cache' 
    $file = "./cache/".$ip; 
    if(!file_exists($file)) { 
        // request 
        $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true"); 
        $f = fopen($file,"w+"); 
        fwrite($f,$json); 
        fclose($f); 
    } else { 
        $json = file_get_contents($file); 
    } 
    
    $json = json_decode($json,true); 
    echo "<pre>"; 
    print_r($json); 
    
    ?>
    

    Demo Link