PHP 클래스 yii\validators\IpValidator

It also may change attribute's value if normalization of IPv6 expansion is enabled. The following are examples of validation rules using this validator: php ['ip_address', 'ip'], // IPv4 or IPv6 address ['ip_address', 'ip', 'ipv6' => false], // IPv4 address (IPv6 is disabled) ['ip_address', 'ip', 'subnet' => true], // requires a CIDR prefix (like 10.0.0.1/24) for the IP address ['ip_address', 'ip', 'subnet' => null], // CIDR prefix is optional ['ip_address', 'ip', 'subnet' => null, 'normalize' => true], // CIDR prefix is optional and will be added when missing ['ip_address', 'ip', 'ranges' => ['192.168.0.0/24']], // only IP addresses from the specified subnet are allowed ['ip_address', 'ip', 'ranges' => ['!192.168.0.0/24', 'any']], // any IP is allowed except IP in the specified subnet ['ip_address', 'ip', 'expandIPv6' => true], // expands IPv6 address to a full notation format
부터: 2.0.7
저자: Dmitry Naumenko ([email protected])
상속: extends Validator
파일 보기 프로젝트 열기: yiisoft/yii2

공개 프로퍼티들

프로퍼티 타입 설명
$expandIPv6 whether to expand an IPv6 address to the full notation format. Defaults to false.
$hasSubnet user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4 whether the validating value can be an IPv4 address. Defaults to true.
$ipv4NotAllowed user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4Pattern Regexp-pattern to validate IPv4 address
$ipv6 whether the validating value can be an IPv6 address. Defaults to true.
$ipv6NotAllowed user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv6Pattern Regexp-pattern to validate IPv6 address
$message user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$negation whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
$networks The network aliases, that can be used in [[ranges]]. - key - alias name - value - array of strings. String can be an IP range, IP address or another alias. String can be negated with [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
$noSubnet user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$normalize whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
$notInRange user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$subnet whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
$wrongCidr user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated

공개 메소드들

메소드 설명
clientValidateAttribute ( $model, $attribute, $view )
getRanges ( ) : array
init ( )
setRanges ( array $ranges ) Set the IPv4 or IPv6 ranges that are allowed or forbidden.
validateAttribute ( $model, $attribute )

보호된 메소드들

메소드 설명
validateIPv4 ( string $value ) : boolean Validates IPv4 address
validateIPv6 ( string $value ) : boolean Validates IPv6 address
validateValue ( $value )

비공개 메소드들

메소드 설명
expandIPv6 ( string $ip ) : string Expands an IPv6 address to it's full notation. For example 2001:db8::1 will be expanded to 2001:0db8:0000:0000:0000:0000:0000:0001
getIpParsePattern ( ) : string Used to get the Regexp pattern for initial IP address parsing
getIpVersion ( string $ip ) : integer Gets the IP version
inRange ( string $ip, integer $cidr, string $range ) : boolean Checks whether the IP is in subnet range
ip2bin ( string $ip ) : string Converts IP address to bits representation
isAllowed ( string $ip, integer $cidr ) : boolean The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list.
parseNegatedRange ( $string ) : array Parses IP address/range for the negation with [[NEGATION_CHAR]].
prepareRanges ( $ranges ) : array Prepares array to fill in [[ranges]]: - Recursively substitutes aliases, described in [[networks]] with their values - Removes duplicates
validateSubnet ( $ip ) : string | array Validates an IPv4/IPv6 address or subnet

메소드 상세

clientValidateAttribute() 공개 메소드

public clientValidateAttribute ( $model, $attribute, $view )

getRanges() 공개 메소드

public getRanges ( ) : array
리턴 array The IPv4 or IPv6 ranges that are allowed or forbidden.

init() 공개 메소드

public init ( )

setRanges() 공개 메소드

The following preparation tasks are performed: - Recursively substitutes aliases (described in [[networks]]) with their values. - Removes duplicates
public setRanges ( array $ranges )
$ranges array the IPv4 or IPv6 ranges that are allowed or forbidden. When the array is empty, or the option not set, all IP addresses are allowed. Otherwise, the rules are checked sequentially until the first match is found. An IP address is forbidden, when it has not matched any of the rules. Example: ```php [ 'ranges' => [ '192.168.10.128' '!192.168.10.0/24', 'any' // allows any other IP addresses ] ] ``` In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` subnet. IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction.

validateAttribute() 공개 메소드

public validateAttribute ( $model, $attribute )

validateIPv4() 보호된 메소드

Validates IPv4 address
protected validateIPv4 ( string $value ) : boolean
$value string
리턴 boolean

validateIPv6() 보호된 메소드

Validates IPv6 address
protected validateIPv6 ( string $value ) : boolean
$value string
리턴 boolean

validateValue() 보호된 메소드

protected validateValue ( $value )

프로퍼티 상세

$expandIPv6 공개적으로 프로퍼티

whether to expand an IPv6 address to the full notation format. Defaults to false.
public $expandIPv6

$hasSubnet 공개적으로 프로퍼티

user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: subnet
public $hasSubnet

$ipv4 공개적으로 프로퍼티

whether the validating value can be an IPv4 address. Defaults to true.
public $ipv4

$ipv4NotAllowed 공개적으로 프로퍼티

user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: ipv4
public $ipv4NotAllowed

$ipv4Pattern 공개적으로 프로퍼티

Regexp-pattern to validate IPv4 address
public $ipv4Pattern

$ipv6 공개적으로 프로퍼티

whether the validating value can be an IPv6 address. Defaults to true.
public $ipv6

$ipv6NotAllowed 공개적으로 프로퍼티

user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: ipv6
public $ipv6NotAllowed

$ipv6Pattern 공개적으로 프로퍼티

Regexp-pattern to validate IPv6 address
public $ipv6Pattern

$message 공개적으로 프로퍼티

user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
public $message

$negation 공개적으로 프로퍼티

whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
public $negation

$networks 공개적으로 프로퍼티

The network aliases, that can be used in [[ranges]]. - key - alias name - value - array of strings. String can be an IP range, IP address or another alias. String can be negated with [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
public $networks

$noSubnet 공개적으로 프로퍼티

user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: subnet
public $noSubnet

$normalize 공개적으로 프로퍼티

whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
또한 보기: subnet
public $normalize

$notInRange 공개적으로 프로퍼티

user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: ranges
public $notInRange

$subnet 공개적으로 프로퍼티

whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
public $subnet

$wrongCidr 공개적으로 프로퍼티

user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
또한 보기: subnet
public $wrongCidr