lionsoul2014/ip2region · error · Exception

invalid ip address `{$ip}`

Error message

invalid ip address `{$ip}`

What it means

Defensive guard in the PHP searcher: Util::parseIP returned null for the input, meaning the string could not be parsed as either IPv4 or IPv6. Note the util parser normally throws on bad syntax, so hitting this null check indicates a parse failure path that yielded null rather than an exception.

Source

Thrown at binding/php/xdb/Searcher.class.php:469

    public function getIPVersion() {
        return $this->version;
    }

    public function getIOCount() {
        return $this->ioCount;
    }

    /**
     * find the region info for the specified ip address.
     * @Note: the ip address couldO ONLY be a human-readable IP address string,
     * DO not use the packed binary string returned by #parseIP
     * 
     * @throws Exception
     */
    public function search($ip) {
        $ipBytes = Util::parseIP($ip);
        if ($ipBytes == null) {
            throw new Exception("invalid ip address `{$ip}`");
        }

        return $this->searchByBytes($ipBytes);
    }

    /**
     * find the region info for the specified binary ip bytes returned by #parseIP.
     * 
     * @throws Exception
     */
    public function searchByBytes($ipBytes) {
        // ip version check
        if (strlen($ipBytes) != $this->version->bytes) {
            throw new Exception("invalid ip address ({$this->version->name} expected)");
        }

        // reset the global counter
        $this->ioCount = 0;

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Pass a valid human-readable IPv4 or IPv6 string (not the packed binary from Util::parseIP)
  2. Pre-validate with filter_var($ip, FILTER_VALIDATE_IP) before searching
  3. Route the query to a searcher whose version matches the address family
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at binding/php/xdb/Searcher.class.php:469 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/d342f2aaf650bbfc. Report an issue: GitHub.