Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 2062

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!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="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Network Functions"
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"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.inet-pton.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.long2ip.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.ip2long"
></A
>ip2long</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN149524"
></A
><P
>    (PHP 4, PHP 5)</P
>ip2long&nbsp;--&nbsp;
      Converts a string containing an (IPv4) Internet Protocol dotted address
      into a proper address
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN149527"
></A
><H2
>Description</H2
>int <B
CLASS="methodname"
>ip2long</B
> ( string ip_address )<BR
></BR
><P
>&#13;     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
>注意: </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="AEN149546"
></A
><P
><B
>例 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">&lt;?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:&lt;br /&gt;\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">"/&lt;br /&gt;\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$out</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </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
>&#13;     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="AEN149557"
></A
><P
><B
>例 2. Displaying an IP address</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$ip&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'Invalid IP, please try again'</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$ip&nbsp;&nbsp;&nbsp;</font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 192.0.34.166<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">echo </font><font color="#0000BB">$long </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// -1073732954<br />&nbsp;&nbsp;&nbsp;&nbsp;</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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     <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="AEN149563"
></A
><P
><B
>例 3. IP validation</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     <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
>注意: </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 &#60;= 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
>&#13;     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"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.long2ip.html"
ACCESSKEY="N"
>下一页</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"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>long2ip</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>