{"record":{"id":"cf8c4ef82e55afae","repo":"yiisoft/yii2","slug":"ipv6-is-not-supported-by-inet-pton","errorCode":null,"errorMessage":"IPv6 is not supported by inet_pton()!","messagePattern":"IPv6 is not supported by inet_pton\\(\\)!","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/helpers/BaseIpHelper.php","lineNumber":115,"sourceCode":"    {\n        $hex = unpack('H*hex', inet_pton($ip));\n        return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);\n    }\n\n    /**\n     * Converts IP address to bits representation.\n     *\n     * @param string $ip the valid IPv4 or IPv6 address\n     * @return string bits as a string\n     * @throws NotSupportedException\n     */\n    public static function ip2bin($ip)\n    {\n        $ipBinary = null;\n        if (static::getIpVersion($ip) === self::IPV4) {\n            $ipBinary = pack('N', ip2long($ip));\n        } elseif (@inet_pton('::1') === false) {\n            throw new NotSupportedException('IPv6 is not supported by inet_pton()!');\n        } else {\n            $ipBinary = inet_pton($ip);\n        }\n\n        $result = '';\n        for ($i = 0, $iMax = strlen($ipBinary); $i < $iMax; $i += 4) {\n            $result .= str_pad(decbin(unpack('N', substr($ipBinary, $i, 4))[1]), 32, '0', STR_PAD_LEFT);\n        }\n        return $result;\n    }\n}\n","sourceCodeStart":97,"sourceCodeEnd":127,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/helpers/BaseIpHelper.php#L97-L127","documentation":"IpHelper::ip2bin() converts addresses to a bit string; for IPv6 it relies on inet_pton(). The method first probes @inet_pton('::1') and, if that probe fails, throws yii\\base\\NotSupportedException — meaning the running PHP build's inet_pton() cannot process IPv6 at all. It is a platform capability check, not an input validation failure.","triggerScenarios":"IpHelper::ip2bin('2001:db8::1') on a PHP compiled against a C library with IPv6-disabled inet_pton (seen on legacy Windows builds and some embedded stacks); IPv6 CIDR checks via IpHelper::inRange() or ip2bin-based comparisons that only receive IPv6 input behind dual-stack load balancers.","commonSituations":"Apps deployed to old Windows/IIS servers or unusual containers where IPv6 support was not compiled in; production-only failures because only prod traffic includes IPv6 clients; local dev on outdated XAMPP/WAMP builds passing all tests while the IPv6 path is never exercised.","solutions":["Upgrade PHP to a build with full IPv6 inet_pton support; verify with var_dump(@inet_pton('::1')); — it must not be false.","Add a startup capability check so the app fails fast with a clear message on incompatible hosts.","Catch NotSupportedException at the IP-processing boundary, log the address family, and skip features that need bit-level IPv6.","If upgrading is impossible, route IPv6 handling through a pure-PHP 128-bit encoder instead of inet_pton()."],"exampleFix":"// before\n$bits = IpHelper::ip2bin($ip); // $ip = '2001:db8::1' on a broken build\n\n// after\nif (@inet_pton('::1') === false) {\n    throw new \\RuntimeException('This PHP build cannot process IPv6 addresses');\n}\n$bits = IpHelper::ip2bin($ip);","handlingStrategy":"validation","validationCode":"if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && @inet_pton('::1') === false) {\n    throw new \\RuntimeException('This platform cannot process IPv6 addresses');\n}\n$bits = \\yii\\helpers\\IpHelper::ip2bin($ip);","typeGuard":null,"tryCatchPattern":"try {\n    $bits = \\yii\\helpers\\IpHelper::ip2bin($ip);\n} catch (\\yii\\base\\NotSupportedException $e) {\n    \\Yii::error('IPv6 processing unavailable on this host: ' . $ip, 'network');\n    throw $e;\n}","preventionTips":["Run a capability probe (@inet_pton('::1')) once at startup and record it in health checks.","Exercise the IPv6 code path in CI against a realistic PHP build, not just local dev.","Know where IPv6 addresses enter (dual-stack load balancers, proxy headers) and validate them early."],"tags":["network","ipv6","php-platform","not-supported","yii2"],"backgroundTag":"ipv6-not-supported","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}