Validate based on whether $ENV{REMOTE_ADDR} is present in a list of IP's or in the IP ranges listed in CIDR notation. Real-world application was within a subfunction, used to restrict signup for an email account.
#!/usr/bin/perl use CGI; use Net::CIDR; open ACL, "< /usr/local/atmail/ACL.txt"; my (@ACL, @CIDRs, $entry, $entry1, $offset, $offset1, $ip, $found); # Create an array of the entries from the list file while(<ACL>){ push(@ACL, $_); } # Push the CIDR's into an array and undef them (avoiding splice() prob +lems with the foreach and offset) in @ACL so they can be easily remov +ed later... foreach $entry (@ACL){ if ($entry =~ /(\d{1,3}\.){3}\d+\//){ push(@CIDRs, $entry); undef $ACL[$offset]; } $offset++; } # Anything left in the list that's not an IP gets whacked foreach $entry1 (@ACL){ if (!(Net::CIDR::cidrvalidate($entry1))){ undef $ACL[$offset1]; } $offset1++; } # Iterate ACL, finishing up if the IP in the HTTP request can be found + in any of the CIDRs or if it matches the current IP in the list. foreach $ip (@ACL){ chomp $ip; if ($ENV{REMOTE_ADDR} eq "$ip" || Net::CIDR::cidrlookup($ENV{REMOT +E_ADDR}, @CIDRs)){ $found = 1; last; } } # If your IP is allowed, do nothing. If not, display error message. if ($found){ } else{ print "Content-Type: text/html\n\n"; print "ERROR YOU CANT SIGNUP"; exit; }

In reply to CGI: Validate access based on IP - with CIDR support by nerfherder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.