php-doc-en/reference/network/functions/ip2long.xml
Jakub Vrana 700f758ccb 255.255.255.255 (bug #29705)
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@166508 c90b9560-bf6c-de11-be94-00142212c4b1
2004-08-16 13:15:22 +00:00

132 lines
3.9 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.13 $ -->
<!-- splitted from ./en/functions/network.xml, last change in rev 1.18 -->
<refentry id="function.ip2long">
<refnamediv>
<refname>ip2long</refname>
<refpurpose>
Converts a string containing an (IPv4) Internet Protocol dotted address
into a proper address
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>int</type><methodname>ip2long</methodname>
<methodparam><type>string</type><parameter>ip_address</parameter></methodparam>
</methodsynopsis>
<para>
The function <function>ip2long</function> generates an IPv4 Internet
network address from its Internet standard format (dotted string)
representation. If <parameter>ip_address</parameter> is invalid then
<literal>-1</literal> is returned. Note that <literal>-1</literal>
does not evaluate as &false; in PHP.
</para>
<note>
<para>
As of PHP 5.0.0 <function>ip2long</function> returns &false; when
<parameter>ip_address</parameter> is invalid.
</para>
</note>
<example>
<title><function>ip2long</function> Example</title>
<programlisting role="php">
<![CDATA[
<?php
$ip = gethostbyname('www.example.com');
$out = "The following URLs are equivalent:<br />\n";
$out .= 'http://www.example.com/, http://' . $ip . '/, and http://' . sprintf("%u", ip2long($ip)) . "/<br />\n";
echo $out;
?>
]]>
</programlisting>
</example>
<para>
<note>
<para>
Because PHP's integer type is signed, and many IP addresses will
result in negative integers, you need to use the "%u" formatter of
<function>sprintf</function> or <function>printf</function> to get
the string representation of the unsigned IP address.
</para>
</note>
</para>
<para>
This second example shows how to print a converted address with the
<function>printf</function> function in both PHP 4 and PHP 5:
</para>
<example>
<title>Displaying an IP address</title>
<programlisting role="php">
<![CDATA[
<?php
$ip = gethostbyname('www.example.com');
$long = ip2long($ip);
if ($long == -1 || $long === FALSE) {
echo 'Invalid IP, please try again';
} else {
echo $ip . "\n"; // 192.0.34.166
echo $long . "\n"; // -1073732954
printf("%u\n", ip2long($ip)); // 3221234342
}
?>
]]>
</programlisting>
</example>
<para>
<function>ip2long</function> should not be used as the sole form
of IP validation. Combine it with <function>long2ip</function>:
</para>
<example>
<title>IP validation</title>
<programlisting role="php">
<![CDATA[
<?php
// make sure IPs are valid. also converts a non-complete IP into
// a proper dotted quad as explained below.
$ip = long2ip(ip2long("127.0.0.1")); // "127.0.0.1"
$ip = long2ip(ip2long("10.0.0")); // "10.0.0.0"
$ip = long2ip(ip2long("10.0.256")); // "10.0.1.0"
?>
]]>
</programlisting>
</example>
<para>
<function>ip2long</function> will also work with non-complete IP
addresses. Read <ulink url="&url.ip2long.tech;">&url.ip2long.tech;</ulink>
for more info.
</para>
<note>
<para>
<function>ip2long</function> will return <literal>-1</literal> (PHP 4) or
&false; (PHP 5) for the IP <literal>255.255.255.255</literal>.
</para>
</note>
<para>
See also <function>long2ip</function> and
<function>sprintf</function>.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->