Detect Country using PHP with IP2Location LITE IP-COUNTRY Database - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, March 7, 2022

Detect Country using PHP with IP2Location LITE IP-COUNTRY Database

Good afternoon community. I have a PHP code to detect IPv4 and show the country of origin, using the "IP2Location LITE IP-COUNTRY Database".

My PHP code does not read IPv6 correctly. Will there be any way to modify it? I can't find a solution in any forum.

I attach my detection code for IPv4:

class CountryFromIP { private $CountryIPDatabase = 'dbIPv6.CSV';

      public function GetCountryName($ip) { 
        $this->ip = $ip;
        $ip = sprintf("%u", ip2long($ip)); 
                                           
 
        $csvArray = file($this->CountryIPDatabase);
 
        for($i=0; $i<count($csvArray); $i++){ 
            $setCsv = str_replace("\"", "", $csvArray[$i]); 
            $arrayOfLine = explode(',', $setCsv); 
            if($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) { 
                return $countryName = $arrayOfLine[3]; 
            } 
        } 
        return false;
    } 
 
     public function PaisSinEspacios() { 
    
        if($countryName = trim(ucwords(strtolower($this->GetCountryName($this->ip))) )) { 
            
            $countryName = str_replace(' ','%20',$countryName);
        }else{
            $countryName = "Pais desconocido"; 
        } 
    } 
} 
$object = new CountryFromIP();
$countryName =  $object->GetCountryName($ip);

I would appreciate any kind of opinion. Best regards.



source https://stackoverflow.com/questions/71372702/detect-country-using-php-with-ip2location-lite-ip-country-database

No comments:

Post a Comment