Language: php (GeSHi-highlighted)
<?php
/*
* Copyright (C) 2007 CrYpTiC MauleR <http://www.expertsrt.net/images/cm_email.gif>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* @desc Converts An IPv4 IP Address To An IPv6 IP Address
* @param str $ip
* @return str
*/
function ipv4_to_ipv6($ip)
{
if (-1 !== ip2long($ip)) /* Check If Valid IPv4 IP */
{
$octets = explode('.', $ip);
$ip = '0:0:0:0:0:0:';
foreach ($octets as $octet => $value)
{
if (2 === $octet)
{
$ip .= ':';
}
$ip .= ltrim(dechex($value), '0'); /* Remove Leading Zeroes */
}
return $ip;
}
return false;
}
?>
I made the above to convert an IPv4 IP to an IPv6 IP with leading zero compression. Also used
http://www.php.net/manual/en/function.ip2long.php#71957 to make IPv6 IPs long so can insert into database more easily and do range checks etc. I'm still not sure if I did the above conversion correctly since I have yet to check out the RFC, so please correct me if I have not. Thanks.