IP/Block/Range List PHP Script
-
I need a very simplified script (no extras, no html parsers, no multi-functioned scripting, etc) with ONE function that I can use to check whether an IP (in standard form, aka 192.168.1.105) is in a special "filter" list which contains numerious entries, in form of single IPs, IP ranges specified as 192.168.1.*, IP ranges specified as 192.168.1.??, and IP ranges specified as 192.168.1.0-100. If the looked up IP belongs to any of the single IP entries, or is within a range of any, then it should return a $TRUE, otherwise a $FALSE. For example, if your function is named chkiplist() and I do : $ipchk1 = chkiplist(24.25.26.27); $ipchk2 = chkiplist(24.26.27.28); $ipchk3 = chkiplist(25.28.27.28); $ipchk3 = chkiplist(26.27.28.29); And the block list is: 24.25.26.27 24.26.*.* 25.26.??.2? 26.27.0-50.0-225 ALL of them should return $TRUE, because each of the entries in the block list corresponds to the IPs looked up, respectively. Also, I would like to be able to place 'comments' in the block list, in form of a : # Comment Here // Another Comment hEre / Another Comment here /* Another Comment here */ ; Another comment here Or you can simply just have the script check whether the line contains an IP/Range, and if it does not, then the line is a comment. Overview: 1) Single working function, not a full script, the simpler, the better 2) Uses a txt filter list, one IP/Range per line 3) IP/Range Format: - 192.168.1.105 (Single IP) - 192.168.1.* (IP Range with *) - 192.168.1.100-110 (IP range with -) - 192.168.?.* (IP range with ?) 4) Script should be able to ignore comments (text) within the block .txt list and skip the comments, but check the IP/Range entries. 5) Script should return a simple $TRUE if the looked up IP matches any of the entries, and a $FALSE if it does not. 6) Script must be clean, but also very short and simple. 7) If possible, have it UNIX/win32 reverse compatible Please let me know if you need me to include any specifications. Thank you.
-
Answer:
Hello theboistard Thank-you for your question. I have pasted the solution you require below. I have commented the script so that you can see what it is doing at each stage - these can be removed once you are happy that you understand this. The script returns true (or "1") if a match is not found, if it is not it returns false (or ""). If you have any questions regarding this function please ask for clarification and I will do my best to help further. <? function chkiplist($ip) { # read in the ip address file $lines = file("ipaddresses.txt"); # set a variable as false $found = false; # loop through the ip address file foreach ($lines as $line) { # remove line feeds from the line $line = chop($line); # replace * with a 3 digit number $line = str_replace("*","\d{1,3}",$line); # replace ? with a single digit $line = str_replace("?","\d",$line); # if it contains a - character if ( strpos($line,"-",0) <> "" ) { # split the line and number you are checking into parts $line_chunks = split(".",$line); $ip_chunks = split("-",$ip); # set a variable to count things $valid = 0; # for each number in the ip address for ( $i=0;$i<4; $i++ ) { # if the chunk contains a - if ( strpos($line_chunks[$i],"-",0) <> "" ) { # if the line chunk is the same as the ip chunk then increment the # counter if ( $line_chunks[$i] == $ip_chunks[$i] ) { $valid++; }; } else { # if no - in the line chunk $splitit = split("-",$line_chunks[$i]); # increment the counter if the chunk is in the range if ( ($splitit[0] >= $ip_chunks[$i]) && ($splitit[1] <= $ip_chunks[$i]) ) { $valid++; }; } }; # if all the parts match then return a match if ( $valid == 4 ) { $found = true; }; } else { # if the line does not contain a - # if the line matches the ip address then return a match if ( preg_match ("|$line|",$ip) ) { $found = true; }; } } return $found; }; # end function ?>
theboistard-ga at Google Answers Visit the source
Related Q & A:
- How do I permanently block a user forever? I mean, find or enter the username, when the user isnt on my list?Best solution by Yahoo! Answers
- How do you block your IP address when emailing people?Best solution by Yahoo! Answers
- How to start creating a php script, that will be installed on many servers?Best solution by Stack Overflow
- How can hotspots support more people than the number of IP's allowed in a typical IPv4 IP range?Best solution by Server Fault
- How do you block ip address?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.