Coding Domain

Perl Modules: Test::IPAddress


Downloads
Perl Modules
Name
Test::IPAddress - Working with IP addresses

Synopsis
use Text::IPAddress;

print "The client IP address is: " . ClientIP();  # for CGI programs

my @BanBlocks = ('255.33.*.*', '146.45.0.1', '143.44.*.*');

if(IsClientIP(@BanBlocks))
{
  print "You're banned!";
  exit;
}

my $BlockNum = IsClientIP(@BanBlocks);
if($BlockNum)
{
  print "Your IP block ($BanBlocks[$BlockNum-1]) is banned!";
  exit;
}

Description
This module can be used to check IP addresses, and it can also find out the client IP address in a CGI environment, based on environment variables used by various webservers.

SUBROUTINES
IsIP(IPAddress)
Tests whether the string is an IP address (###.###.###.###).

IsIPBlock(IPAddress)
Tests whether the string is an IP block (###.###.###.### or ###.*.*.*).

IsLocalIP(IPAddress)
Compares the string with the local IP address (127.0.0.1).

IsIPInBlock(IPAddress, IPBlock1, IPBlock2, IPBlockN)
Compares the IP address with the IP blocks. Returns the block number that matched the IP address. Returns 0 on failure (false)

Sample:

my $IP = '243.56.22.111';
my $Block = IsIPInBlock($IP, '127.*.*.*', '255.33.*.*');

if($Block) { print "The IP $IP is in block $Block" }
else       { print "The IP does not match any block!" }

ClientIP()
Returns the IP address of the client, based on various IP environment variables. This can properly be used only in CGI environments.

IsClientIP(IPBlocks)
Compares the string with the client IP address. This can properly be used only in CGI environments.

Author
Copyright (c) 2001, Diederik van der Boor - All Rights Reserved