首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >例外- Maxmind geoip2

例外- Maxmind geoip2
EN

Stack Overflow用户
提问于 2021-09-30 20:43:36
回答 1查看 196关注 0票数 0

我正在使用Maxmind GeoIP2监控我网站上的流量,但我的脚本在出现致命错误时停止(我在12.34.56.78上更改了IP地址):

代码语言:javascript
复制
PHP Notice:  Undefined index: domain in /home/www/parser_only/ins.php on line 15
PHP Notice:  Undefined index: domain in /home/www/parser_only/ins.php on line 15
PHP Fatal error:  Uncaught GeoIp2\Exception\AddressNotFoundException: The address 12.34.56.78 is not in the database. in /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php:248
Stack trace:
#0 /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php(217): GeoIp2\Database\Reader->getRecord('Country', 'Country', '12.34.56.78')
#1 /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php(90): GeoIp2\Database\Reader->modelFor('Country', 'Country', '12.34.56.78')
#2 /home/www/parser_only/ins.php(115): GeoIp2\Database\Reader->country('12.34.56.78')
#3 /home/www/parser_only/ins.php(73): geo('12.34.56.78')
#4 {main}
  thrown in /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php on line 248
run done

供应商中提到的代码部分:

代码语言:javascript
复制
    /**
     * This method returns a GeoIP2 Country model.
     *
     * @param string $ipAddress an IPv4 or IPv6 address as a string
     *
     * @throws \GeoIp2\Exception\AddressNotFoundException  if the address is
     *                                                     not in the database
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
     *                                                     is corrupt or invalid
     *
     * @return \GeoIp2\Model\Country
     */
    public function country($ipAddress)
    {
        return $this->modelFor('Country', 'Country', $ipAddress);
    }
代码语言:javascript
复制
    private function modelFor($class, $type, $ipAddress)
    {
        list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);

        $record['traits']['ip_address'] = $ipAddress;
        $record['traits']['prefix_len'] = $prefixLen;

        $class = 'GeoIp2\\Model\\' . $class;

        return new $class($record, $this->locales);
    }
代码语言:javascript
复制
    private function getRecord($class, $type, $ipAddress)
    {
        if (strpos($this->dbType, $type) === false) {
            $method = lcfirst($class);
            throw new \BadMethodCallException(
                "The $method method cannot be used to open a {$this->dbType} database"
            );
        }
        list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
        if ($record === null) {
            throw new AddressNotFoundException(
                "The address $ipAddress is not in the database."
            );
        }
        if (!\is_array($record)) {
            // This can happen on corrupt databases. Generally,
            // MaxMind\Db\Reader will throw a
            // MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
            // the lookup may result in a record that looks valid but is not
            // an array. This mostly happens when the user is ignoring all
            // exceptions and the more frequent InvalidDatabaseException
            // exceptions go unnoticed.
            throw new InvalidDatabaseException(
                "Expected an array when looking up $ipAddress but received: "
                . \gettype($record)
            );
        }

        return [$record, $prefixLen];
    }

我的代码是:

代码语言:javascript
复制
$sql .= "('". mysqli_real_escape_string($db, inet_pton($a[2])) ."','". geo($a[2]) ."','". mysqli_real_escape_string($db, trim(substr($a[4], 0, strpos($a[4], 'HTTP')))) ."','". strtotime($a[3]) ."','{$a[6]}','{$a[1]}'),";
代码语言:javascript
复制
function geo($ip){
    $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');

    // Replace "city" with the appropriate method for your database, e.g.,
    // "country".
    $record = $reader->country($ip);

    $geo = $record->country->isoCode . "\n";
    // $geo = geoip_record_by_name($ip);
     if($geo) return $geo;
     else return 'XX';
}

我完全是菜鸟,但我知道我需要在某个地方实现这个TRY-CATCH:https://www.w3schools.com/php/php_exception.asp

有没有人能告诉我是怎么做的,是哪一部分?我想在最后一段代码中。但我不确定这些供应商部件到底是如何工作的,因为已经有一些例外。

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2021-09-30 21:24:40

像这样的东西

代码语言:javascript
复制
function geo($ip) {
    $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');

    // Replace "city" with the appropriate method for your database, e.g.,
    // "country".
    try {
        $record = $reader->country($ip);
    } catch (\GeoIp2\Exception\AddressNotFoundException) {
        return 'XX';
    }

    $geo = $record->country->isoCode . "\n";
    // $geo = geoip_record_by_name($ip);
     if($geo) return $geo;
     else return 'XX';
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69398633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档