php - IP Address Validation Help -


I am using this IP authentication function which came when browsing, it is working fine till date I have a problem

For some reason this work will not valid as this IP valid: 203.81.192.26

I am not very good with regular expressions, so what

If you have any other work, then I would appreciate it if you can post it for me. The code for the

function is given below:

  public static work valid IP address ($ ip_addr) {global $ errors; $ Preg = '# ^ (?:??: 25 [0-5] | 2 [0-4] [0-9] | [01] [0- 9] [0-9]?).) {3 } '' (: 25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9]?) $ # '; If (preg_match ($ preg, $ ip_addr)) {// Now all curved values ​​are exposed to $ parts = (".", $ Ip_addr); // Now we need to check each part of the 0-255 foreign currency ($ ip_parts as $ parts) {if (intval ($ ip_parts)> gt; 255 || intval ($ ip_parts) <0) {$ Errors [] = "The IP address is not valid."; return false; } Back true; } Back true; } And {$ errors [] = "Please double-check the IP address."; return false; }}  

OK, why are you both regex and int comparisons? You are "double" to check the address. Also, your second check is not valid because it will always be true if the first octet is valid (you have a refund inside the foreign currency loop).

You can:

  $ parts = explosion ('.', $ Ip_addr); If (count ($ parts) == 4) {foreach ($ part $ part) {if ($ part> 255 || $ part & lt; 0) {// error}} back true; } Other {return false; }  

But as suggested by others, ip2long / long2ip can better suit your needs ...


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -