<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AndrewEnsley.com &#187; Validation</title>
	<atom:link href="http://andrewensley.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewensley.com</link>
	<description>My little contribution to the WWW</description>
	<lastBuildDate>Thu, 08 Dec 2011 21:37:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<cloud domain='andrewensley.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
	<a href="http://andrewensley.com/seaworthy.php" style="padding:0;margin:0;" rel="nofollow"><div style="display:none;">support</div></a><a href="http://andrewensley.com/seaworthy.php" style="padding:0;margin:0;" rel="nofollow"><img border="0" width="0" height="0" style="padding:0;margin:0;" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a>	<item>
		<title>PHP: Validate an IP Address</title>
		<link>http://andrewensley.com/2010/03/php-validate-an-ip-address/</link>
		<comments>http://andrewensley.com/2010/03/php-validate-an-ip-address/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 05:14:47 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Data Filtering]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[IP address]]></category>
		<category><![CDATA[IPv4]]></category>
		<category><![CDATA[IPv6]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Regular expression]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://andrewensley.com/?p=1329</guid>
		<description><![CDATA[<span style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">partner</a></span><a href="http://www.medwitness.com/contact.php?pk=518" style="padding:0;margin:0;" rel="nofollow"><span style="display:none;">suggest</span></a><div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">podcast</a></div><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">participate</a></div><a href="http://andrewensley.com/seaworthy.php" style="padding:0;margin:0;" rel="nofollow"><span style="display:none;">feed</span></a>So you need to check if some string is a valid IP address. You could simply test it against a regular expression: 1234567function is_valid_ipv4&#40;$ip&#41; &#123; &#160; &#160; return preg_match&#40;'/\b(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\b/', $ip&#41; !== 0; &#125; Regular Expression obtained here This will actually [...]<p><!--9cecd8a91e2c4435b314449a6dc5f1a1--></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">copyright</a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">store</a><span style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">notice</a></span><span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">mail</a></span><a href="http://andrewensley.com/seaworthy.php" style="padding:0;margin:0;" rel="nofollow"><!-- careers --></a><p>So you need to check if some string is a valid IP address.  You could simply test it against a regular expression:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> is_valid_ipv4<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><a href="http://www.regular-expressions.info/examples.html" target="_blank" title="Regular Expression Examples" class="extlink">Regular Expression obtained here</a></p>
<p>This will actually work for most situations, but it's lacking in a few ways.  Suppose you want to exclude <a href="http://en.wikipedia.org/wiki/Private_network" title="Private Network - Wikipedia" target="_blank" class="extlink">private or reserved IP addresses</a>.  Maybe you want to validate IPv6 addresses too; not just IPv4.</p>
<p>Enter <a href="http://us2.php.net/manual/en/book.filter.php" target="_blank" title="PHP's Data Filtering Extension" class="extlink">PHP's Data Filtering Extension</a>.  It just works, and you don't have to worry about maintaining (or properly applying) complex regular expressions.</p>
<p><span id="more-1329"></span></p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> is_valid_ip<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> <span style="color: #000088;">$include_priv_res</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$include_priv_res</span> ?<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #339933;">,</span> FILTER_FLAG_NO_PRIV_RANGE <span style="color: #339933;">|</span> FILTER_FLAG_NO_RES_RANGE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now to test it.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$ips</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'72.215.140.69'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'192.168.0.1'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'10.0.0.1'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'255.255.255.0'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'andrewensley.com'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'255.255.256.0'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'::1'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'fe00::0'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'2001:4860:0:1001::68'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ips</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> is_valid_ip<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The above will output:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">72.215.140.69 yes<br />
192.168.0.1 no<br />
127.0.0.1 yes<br />
10.0.0.1 no<br />
255.255.255.0 no<br />
andrewensley.com no<br />
255.255.256.0 no<br />
::1 yes<br />
fe00::0 yes<br />
2001:4860:0:1001::68 yes</div></td></tr></tbody></table></div>
<p>Thank you PHP for making this otherwise complex task very simple.  And even better: it's fast!</p>
<p>I tested the regular expression and the filter_var functions by running each 100,000 times.  Here are the timings for each test:</p>
<ul>
<li>1.53423094749 seconds with the filter_var function</li>
<li>3.1516289711 seconds with the regular expression function</li>
</ul>
<p>Not bad!  Happy filtering.</p>
<p>Possibly Related posts:<ol>
<li><a href='http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/' rel='bookmark' title='PHP and FFMPEG - Performing intelligent video conversion'>PHP and FFMPEG - Performing intelligent video conversion</a></li>
<li><a href='http://andrewensley.com/2010/04/recover-wordpress-from-broken-theme/' rel='bookmark' title='Recover WordPress from broken theme'>Recover WordPress from broken theme</a></li>
</ol></p><a href="http://www.medwitness.com/contact.php?pk=518" style="padding:0;margin:0;" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0" style="padding:0;margin:0;" /></a>]]></content:encoded>
			<wfw:commentRss>http://andrewensley.com/2010/03/php-validate-an-ip-address/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	<a href="http://www.medwitness.com/contact.php?pk=518" style="padding:0;margin:0;" rel="nofollow"><img border="0" width="0" height="0" style="padding:0;margin:0;" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a></channel>
</rss>

