Checks whether the specified IP address satisfies the selected validation rule and IP format.
Syntax
public static function ip_address ($str, $valid, $valid_formats)
Parameters
str
A string value that specifies the IP address to check.
valid
A string value that specifies the validation rule to apply. Is set to IP_ADDRESS_ANY by default. The following validation rules are available:
valid_formats
A string value that specifies the allowed IP address format. Is set to IP_ADDRESS_ANY_FORMAT by default. The following formats are available:
xxx.xxx.xxx.xxx/n where n is a number of '1' network bits in the mask).xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx (<IP_address>/<IP_mask>). xxx.xxx.xxx.xxx. Returns
A boolean which is true if the checked IP address satisfies the validation rule(s) applied and is formatted as specified by the valid_formats constant. Otherwise returns false.
Remarks
This function validates the passed in IP address based on two constants that specify the format of the IP address and the validation rule(s) that should be applied to discover whether the IP address in focus really matches this format.
Here is the table of the admissible combinations of the validation rules and formats:
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The validation rule(s) to apply is (are) specified in the valid parameter. They work as follows:
123.123.0.0/16 is a range of addresses, but 123.123.0.1/16 is a particular IP address in a class B network.123.123.123.1/32 or 123.123.123.1/255.255.255.255. The rule fails if the IP address has '0' bits in its host part, e.g. 123.123.0.0/16, or if it has all '1' bits in the host part (a broadcast address), e.g. 123.123.123.255/255.255.255.0. 123.123.123.123.The valid_formats parameter informs the function what IP address format is validated.
123.123.123.*. 123.123.0.0/16 (16 leftmost bits are the network ones). Or this format can be applied to specify a particular IP address and to provide it with the information about the network size, e.g. 123.123.123.123/24 (the host part is the rightmost block of 8 bits). If used to specify a particular non-masked IP address, this format should look as follows: 123.123.123.123/32. xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx). The first part specifies the IP address, the second part presents a mask applied to this address. This format can be used to specify a group of IP addresses (address block), e.g. 123.123.123.0/255.255.255.0, a masked IP address, e.g. 123.123.123.123/255.255.255.0, or a non-masked IP address, e.g. 123.123.123.123/255.255.255.255.xxx.xxx.xxx.xxx). It is used to specify an individual IP address, no information about the network size provided. E.g. 123.123.123.1.