<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.inet-pton.html">inet_pton</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.long2ip.html">long2ip</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.ip2long" class="refentry"> <div class="refnamediv"> <h1 class="refname">ip2long</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ip2long</span> — <span class="dc-title">Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address</span></p> </div> <div class="refsect1 description" id="refsect1-function.ip2long-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>ip2long</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$ip_address</code></span> )</div> <p class="para rdfs-comment"> The function <span class="function"><strong>ip2long()</strong></span> generates an IPv4 Internet network address from its Internet standard format (dotted string) representation. </p> <p class="para"> <span class="function"><strong>ip2long()</strong></span> will also work with non-complete IP addresses. Read <a href="http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm" class="link external">» http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm</a> for more info. </p> </div> <div class="refsect1 parameters" id="refsect1-function.ip2long-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">ip_address</code></em></span> <dd> <p class="para"> A standard format address. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ip2long-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the IPv4 address or <strong><code>FALSE</code></strong> if <em><code class="parameter">ip_address</code></em> is invalid. </p> </div> <div class="refsect1 changelog" id="refsect1-function.ip2long-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.2.10</td> <td> Prior to this version, <span class="function"><strong>ip2long()</strong></span> would sometimes return a valid number even if passed an value which was not an (IPv4) Internet Protocol dotted address. </td> </tr> <tr> <td>5.0.0</td> <td> Prior to this version, <span class="function"><strong>ip2long()</strong></span> returned -1 on failure. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.ip2long-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4491"> <p><strong>Example #1 <span class="function"><strong>ip2long()</strong></span> Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$ip </span><span style="color: #007700">= </span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$out </span><span style="color: #007700">= </span><span style="color: #DD0000">"The following URLs are equivalent:<br />\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out </span><span style="color: #007700">.= </span><span style="color: #DD0000">'http://www.example.com/, http://' </span><span style="color: #007700">. </span><span style="color: #0000BB">$ip </span><span style="color: #007700">. </span><span style="color: #DD0000">'/, and http://' </span><span style="color: #007700">. </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u"</span><span style="color: #007700">, </span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"/<br />\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$out</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4492"> <p><strong>Example #2 Displaying an IP address</strong></p> <div class="example-contents"><p> This second example shows how to print a converted address with the <span class="function"><a href="function.printf.html" class="function">printf()</a></span> function in both PHP 4 and PHP 5: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$ip </span><span style="color: #007700">= </span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$long </span><span style="color: #007700">= </span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$long </span><span style="color: #007700">== -</span><span style="color: #0000BB">1 </span><span style="color: #007700">|| </span><span style="color: #0000BB">$long </span><span style="color: #007700">=== </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Invalid IP, please try again'</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #0000BB">$ip </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// 192.0.34.166<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">$long </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// -1073732954<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 3221234342<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.ip2long-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Because PHP's <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the "%u" formatter of <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> or <span class="function"><a href="function.printf.html" class="function">printf()</a></span> to get the string representation of the unsigned IP address. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><strong>ip2long()</strong></span> will return <strong><code>FALSE</code></strong> for the IP <em>255.255.255.255</em> in PHP 5 <= 5.0.2. It was fixed in PHP 5.0.3 where it returns <em>-1</em> (same as PHP 4). </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.ip2long-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.long2ip.html" class="function" rel="rdfs-seeAlso">long2ip()</a> - Converts an (IPv4) Internet network address into a string in Internet standard dotted format</span></li> <li class="member"> <span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.inet-pton.html">inet_pton</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.long2ip.html">long2ip</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>