From c20f449783dfc0affc83e89f7740cadde53e6462 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 28 Mar 2016 10:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84validate=E7=B1=BB=E7=9A=84ip?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/think/Validate.php b/library/think/Validate.php index 9fc5c1f1..52330ade 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -548,6 +548,21 @@ class Validate return checkdnsrr($value, $rule); } + /** + * 验证是否有效IP + * @access protected + * @param mixed $value 字段值 + * @param mixed $rule 验证规则 ipv4 ipv6 + * @return bool + */ + protected function ip($value, $rule) + { + if (!in_array($rule, ['ipv4', 'ipv6'])) { + $rule = 'ipv4'; + } + return $this->filter($value, FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4); + } + /** * 验证请求类型 * @access protected