<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >ip2long</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Network - Funções de Rede" HREF="ref.network.html"><LINK REL="PREVIOUS" TITLE="inet_pton" HREF="function.inet-pton.html"><LINK REL="NEXT" TITLE="long2ip" HREF="function.long2ip.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.inet-pton.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.long2ip.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.ip2long" ></A >ip2long</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN144291" ></A ><P > (PHP 4, PHP 5)</P >ip2long -- Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address </DIV ><DIV CLASS="refsect1" ><A NAME="AEN144294" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >ip2long</B > ( string ip_address )<BR ></BR ><P > The function <B CLASS="function" >ip2long()</B > generates an IPv4 Internet network address from its Internet standard format (dotted string) representation. If <CODE CLASS="parameter" >ip_address</CODE > is invalid then <TT CLASS="literal" >-1</TT > is returned. Note that <TT CLASS="literal" >-1</TT > does not evaluate as <TT CLASS="constant" ><B >FALSE</B ></TT > in PHP. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > As of PHP 5.0.0 <B CLASS="function" >ip2long()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT > when <CODE CLASS="parameter" >ip_address</CODE > is invalid. </P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN144313" ></A ><P ><B >Exemplo 1. <B CLASS="function" >ip2long()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ip </font><font color="#007700">= </font><font color="#0000BB">gethostbyname</font><font color="#007700">(</font><font color="#DD0000">'www.example.com'</font><font color="#007700">);<br /></font><font color="#0000BB">$out </font><font color="#007700">= </font><font color="#DD0000">"The following URLs are equivalent:<br />\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$out </font><font color="#007700">.= </font><font color="#DD0000">'http://www.example.com/, http://' </font><font color="#007700">. </font><font color="#0000BB">$ip </font><font color="#007700">. </font><font color="#DD0000">'/, and http://' </font><font color="#007700">. </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"%u"</font><font color="#007700">, </font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#0000BB">$ip</font><font color="#007700">)) . </font><font color="#DD0000">"/<br />\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$out</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of <A HREF="function.sprintf.html" ><B CLASS="function" >sprintf()</B ></A > or <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A > to get the string representation of the unsigned IP address. </P ></BLOCKQUOTE ></DIV > </P ><P > This second example shows how to print a converted address with the <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A > function in both PHP 4 and PHP 5: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN144324" ></A ><P ><B >Exemplo 2. Displaying an IP address</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ip </font><font color="#007700">= </font><font color="#0000BB">gethostbyname</font><font color="#007700">(</font><font color="#DD0000">'www.example.com'</font><font color="#007700">);<br /></font><font color="#0000BB">$long </font><font color="#007700">= </font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#0000BB">$ip</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">$long </font><font color="#007700">== -</font><font color="#0000BB">1 </font><font color="#007700">|| </font><font color="#0000BB">$long </font><font color="#007700">=== </font><font color="#0000BB">FALSE</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'Invalid IP, please try again'</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#0000BB">$ip </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">; </font><font color="#FF8000">// 192.0.34.166<br /> </font><font color="#007700">echo </font><font color="#0000BB">$long </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">; </font><font color="#FF8000">// -1073732954<br /> </font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%u\n"</font><font color="#007700">, </font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#0000BB">$ip</font><font color="#007700">)); </font><font color="#FF8000">// 3221234342<br /></font><font color="#007700">}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > <B CLASS="function" >ip2long()</B > should not be used as the sole form of IP validation. Combine it with <A HREF="function.long2ip.html" ><B CLASS="function" >long2ip()</B ></A >: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN144330" ></A ><P ><B >Exemplo 3. IP validation</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// make sure IPs are valid. also converts a non-complete IP into<br />// a proper dotted quad as explained below.<br /></font><font color="#0000BB">$ip </font><font color="#007700">= </font><font color="#0000BB">long2ip</font><font color="#007700">(</font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#DD0000">"127.0.0.1"</font><font color="#007700">)); </font><font color="#FF8000">// "127.0.0.1"<br /></font><font color="#0000BB">$ip </font><font color="#007700">= </font><font color="#0000BB">long2ip</font><font color="#007700">(</font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#DD0000">"10.0.0"</font><font color="#007700">)); </font><font color="#FF8000">// "10.0.0.0"<br /></font><font color="#0000BB">$ip </font><font color="#007700">= </font><font color="#0000BB">long2ip</font><font color="#007700">(</font><font color="#0000BB">ip2long</font><font color="#007700">(</font><font color="#DD0000">"10.0.256"</font><font color="#007700">)); </font><font color="#FF8000">// "10.0.1.0"<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > <B CLASS="function" >ip2long()</B > 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" TARGET="_top" >http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm</A > for more info. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > <B CLASS="function" >ip2long()</B > will return <TT CLASS="constant" ><B >FALSE</B ></TT > for the IP <TT CLASS="literal" >255.255.255.255</TT > in PHP 5 <= 5.0.2. It was fixed in PHP 5.0.3 where it returns <TT CLASS="literal" >-1</TT > (same as PHP 4). </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.long2ip.html" ><B CLASS="function" >long2ip()</B ></A > and <A HREF="function.sprintf.html" ><B CLASS="function" >sprintf()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.inet-pton.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.long2ip.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >inet_pton</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.network.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >long2ip</TD ></TR ></TABLE ></DIV ></BODY ></HTML >