WP_Http::is_ip_address() WordPress Method

The WP_Http::is_ip_address() method is used to check if a given string is an IP address. This can be useful for validating user input, or for checking if a given IP address is allowed to access a certain resource.

WP_Http::is_ip_address( string $maybe_ip ) #

Determines if a specified string represents an IP address or not.


Description

This function also detects the type of the IP address, returning either ‘4’ or ‘6’ to represent a IPv4 and IPv6 address respectively. This does not verify if the IP is a valid IP, only that it appears to be an IP address.


Top ↑

Parameters

$maybe_ip

(string)(Required)A suspected IP address.


Top ↑

Return

(int|false) Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure


Top ↑

Source

File: wp-includes/class-wp-http.php

	public static function is_ip_address( $maybe_ip ) {
		if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $maybe_ip ) ) {
			return 4;
		}

		if ( false !== strpos( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) ) {
			return 6;
		}

		return false;
	}


Top ↑

Changelog

Changelog
VersionDescription
3.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.