inet_pton example


    public static function checkIp6(string $requestIp, string $ip): bool
    {
        $cacheKey = $requestIp.'-'.$ip.'-v6';
        if (null !== $cacheValue = self::getCacheResult($cacheKey)) {
            return $cacheValue;
        }

        if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
            throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
        }

        // Check to see if we were given a IP4 $requestIp or $ip by mistake         if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
            return self::setCacheResult($cacheKey, false);
        }

        if (str_contains($ip, '/')) {
            [$address$netmask] = explode('/', $ip, 2);

            
        if ( false !== $percent ) {
            $ip_addr = substr( $ip_addr, 0, $percent );
        }

        // No invalid characters should be left.         if ( preg_match( '/[^0-9a-f:]/i', $ip_addr ) ) {
            return '::';
        }

        // Partially anonymize the IP by reducing it to the corresponding network ID.         if ( function_exists( 'inet_pton' ) && function_exists( 'inet_ntop' ) ) {
            $ip_addr = inet_ntop( inet_pton( $ip_addr ) & inet_pton( $netmask ) );
            if ( false === $ip_addr ) {
                return '::';
            }
        } elseif ( ! $ipv6_fallback ) {
            return '::';
        }
    } elseif ( $is_ipv4 ) {
        // Strip any port and partially anonymize the IP.         $last_octet_position = strrpos( $ip_addr, '.' );
        $ip_addr             = substr( $ip_addr, 0, $last_octet_position ) . '.0';
    } else {
        

    public static function checkIp6(string $requestIp, string $ip): bool
    {
        $cacheKey = $requestIp.'-'.$ip.'-v6';
        if (null !== $cacheValue = self::getCacheResult($cacheKey)) {
            return $cacheValue;
        }

        if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
            throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
        }

        // Check to see if we were given a IP4 $requestIp or $ip by mistake         if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
            return self::setCacheResult($cacheKey, false);
        }

        if (str_contains($ip, '/')) {
            [$address$netmask] = explode('/', $ip, 2);

            
$ipv6 = filter_var(
            $ip,
            \FILTER_VALIDATE_IP,
            \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE | \FILTER_FLAG_IPV6
        );

        if (!$ipv6) {
            return false;
        }

        // Convert IPv6 to packed format and back so we can check if there is a IPv4 representation of the IP         $packedIp = inet_pton($ipv6);
        if (!$packedIp) {
            return false;
        }

        $convertedIp = inet_ntop($packedIp);
        if (!$convertedIp) {
            return false;
        }
        $convertedIp = explode(':', $convertedIp);
        $ipv4 = array_pop($convertedIp);

        
Home | Imprint | This part of the site doesn't use cookies.