/**
  * Fetches the location from the Maxmind local db
  *
  * @param string $ip
  */
 function getLocation($location_map)
 {
     if (!$this->isDbReady()) {
         return $location_map;
     }
     if (!array_key_exists('ip_address', $location_map)) {
         return $location_map;
     }
     $reader = new Reader($this->db_file_path);
     $record = $reader->get(trim($location_map['ip_address']));
     $reader->close();
     if ($record) {
         $location_map = $this->mapCityRecord($record, $location_map);
     }
     return $location_map;
 }
Beispiel #2
0
 /**
  * @param string|null $ip
  * @return Result
  */
 public function ip($ip = null)
 {
     if ($ip === null) {
         $ip = Yii::$app->request->getUserIP();
     }
     if (!array_key_exists($ip, $this->result)) {
         $key = self::className() . ':' . $ip;
         if ($this->session->offsetExists($key)) {
             $this->result[$ip] = $this->session->get($key);
         } else {
             $result = $this->reader->get($ip);
             $this->result[$ip] = new Result($result);
             $this->session->set($key, $this->result[$ip]);
         }
     }
     return $this->result[$ip];
 }
 public function handle(UserCreateCommand $command)
 {
     $reader = new Reader(storage_path() . '/geolite2city.mmdb');
     $ip = null;
     if (isset($_SERVER['REMOTE_ADDR'])) {
         $ip = $_SERVER['REMOTE_ADDR'];
     }
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
     }
     $country = null;
     $timezone = null;
     if (!is_null($ip)) {
         $record = $reader->get($ip);
         $timezone = Timezone::firstOrCreate(['name' => $record["location"]["time_zone"]]);
         $countryCode = $record["country"]["iso_code"];
         $country = Country::where('iso_3166_2', $countryCode)->first();
     }
     $user = new User(['username' => $command->username, 'email' => $command->email, 'country_id' => is_null($country) ? null : $country->id, 'timezone_id' => is_null($timezone) ? null : $timezone->id]);
     $user->password = $command->password;
     $user->save();
     \Event::fire(new UserCreatedEvent($user));
     return $user->id;
 }
 private function checkIpV6(Reader $reader, $fileName)
 {
     $subnets = array('::1:ffff:ffff', '::2:0:0', '::2:0:40', '::2:0:50', '::2:0:58');
     foreach ($subnets as $address) {
         $this->assertEquals(array('ip' => $address), $reader->get($address), 'found expected data record for ' . $address . ' in ' . $fileName);
     }
     $pairs = array('::2:0:1' => '::2:0:0', '::2:0:33' => '::2:0:0', '::2:0:39' => '::2:0:0', '::2:0:41' => '::2:0:40', '::2:0:49' => '::2:0:40', '::2:0:52' => '::2:0:50', '::2:0:57' => '::2:0:50', '::2:0:59' => '::2:0:58');
     foreach ($pairs as $keyAddress => $valueAddress) {
         $this->assertEquals(array('ip' => $valueAddress), $reader->get($keyAddress), 'found expected data record for ' . $keyAddress . ' in ' . $fileName);
     }
     foreach (array('1.1.1.33', '255.254.253.123', '89fa::') as $ip) {
         $this->assertNull($reader->get($ip));
     }
 }
<?php

require_once '../vendor/autoload.php';
use MaxMind\Db\Reader;
$reader = new Reader('GeoIP2-City.mmdb');
$count = 10000;
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
    $ip = long2ip(rand(0, pow(2, 32) - 1));
    $t = $reader->get($ip);
    if ($i % 1000 == 0) {
        print $i . ' ' . $ip . "\n";
        // print_r($t);
    }
}
$endTime = microtime(true);
$duration = $endTime - $startTime;
print 'Requests per second: ' . $count / $duration . "\n";
Beispiel #6
0
<?php

require_once 'vendor/autoload.php';
use MaxMind\Db\Reader;
$ipAddress = '195.66.90.65';
$databaseFile = 'GeoLite2-City.mmdb';
$reader = new Reader($databaseFile);
print_r($reader->get($ipAddress));
$geoinfo = $reader->get($ipAddress);
print "IP: {$ipAddress}\n";
print "CITY:" . $geoinfo[city][names][en] . "\n";
print "COUNTRY:" . $geoinfo[registered_country][names][en] . "\n";
print "LATITUDE:" . $geoinfo[location][latitude] . "\n";
print "LONGITUDE:" . $geoinfo[location][longitude] . "\n";
$handle = fopen("ips.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        //print "$line";
        $ipAddress = trim($line);
        $geoinfo = $reader->get($ipAddress);
        print "IP: {$ipAddress},";
        print "CITY:" . $geoinfo[city][names][en] . ",";
        print "COUNTRY:" . $geoinfo[registered_country][names][en] . ",";
        print "LATITUDE:" . $geoinfo[location][latitude] . ",";
        print "LONGITUDE:" . $geoinfo[location][longitude] . "\n";
    }
    fclose($handle);
} else {
    // error opening the file.
}
// foreach($reader->get($ipAddress) as $x=>$x_value)