PHP: Validate an IP Address

So you need to check if some string is a valid IP address. You could simply test it against a regular expression:

[cc lang=’php’ ]
function is_valid_ipv4($ip)
{
return preg_match(‘/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/’, $ip) !== 0;
}
[/cc]
Regular Expression obtained here

This will actually work for most situations, but it’s lacking in a few ways. Suppose you want to exclude private or reserved IP addresses. Maybe you want to validate IPv6 addresses too; not just IPv4.

Enter PHP’s Data Filtering Extension. It just works, and you don’t have to worry about maintaining (or properly applying) complex regular expressions.

Read More

RFC Standard for the transmission of IP datagrams on Avian Carriers

Nerd alert: This post contains off-the-charts geek humor. You’ve been warned.

Thanks to my friend who sent me this.

Uber-geeks will recognize “RFC” (yet another TLA) as short for “Request For Comments.” But those with lives normal folks might need an explanation.

The Internet Engineering Task Force (IETF) has created RFC as a standard for documentation of Internet-related subjects. There is an RFC for just about everything, including:

A lot of RFC’s are standards, but the vast majority of them are not. Almost all of them, though, are painfully dry reads. That said, there are some glaring exceptions.

Read More

C# Detect Windows OS Version – Part 2 (WMI)

Recap

In case you missed Part 1, I briefly summarized 4 different strategies for programmatically determining the current Operating System in C#.  The code I posted there utilizes the System.Environment class.  It works great and is sufficient for most uses, but sometimes you want need to know everything about the OS, including the edition (i.e. “XP Pro” vs. “XP Home”).

Summary

For that, Microsoft gives us the lovely Windows Management Instrumentation (WMI) Interface.  You can do a lot of really powerful and interesting things with WMI and WQL, but for this purpose, all we care about is getting some information from it.

Read More