Re: about PHP's filter_var function
| From: | Jim Lucas | Date: | Thu, 20 Sep 2012 17:54:35 +0000 |
| Subject: | Re: about PHP's filter_var function | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319165@lists.php.net to get a copy of this message | ||
On 09/20/2012 10:00 AM, Matijn Woudt wrote:
On Thu, Sep 20, 2012 at 6:03 PM, Jim Lucas<lists@cmsws.com> wrote:After some testing, I stand corrected. Wow, I wonder where I ran into the issue of negative numbers equating to false. while loops maybe... Strange. I must have ran into this issue years ago. I have always performed strict (===) comparisons because I thought PHP would equate negative numbers as false. Learn something new every day... -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/On 09/20/2012 02:35 AM, Sebastian Krebs wrote:First of all, I agree with Maciek that inet_pton is the way to go because of IPv6. But, there seems to be some wrong information in your reply which bothers me. First of all, ip2long only returns negative numbers on 32bit systems, not on 64bit (which most servers are nowadays). Second, there's nothing wrong with the if, if(-5) is still true. The only difference is that you can differentiate between IP 0.0.0.0 and false. But IP 0.0.0.0 is not valid anyway. - MatijnPlaseplease update... 5.1.6 is from 2006! I read the "it's required", but I can't imagine _anything_ that it's worth it to use such an extremely outdated, unsupported and therefore insecure and inefficient version... You know: There are 3 (!) new minor versions available right now (5.2, 5.3 and 5.4). However: Regarding your concrete problem I guess you can use ip2long() if (ip2long($ip)) {I would suggest a modification to this. if ( ip2long($ip) !== false ) { I suggest this because IP to long will return negative numbers for half the IP range. Therefor 50% of your possible results would be considered false when in fact they are valid IPs. See Example #2 on this page: http://php.net/manual/en/function.ip2long.php