<?
/* Storeip -storeip.php-
NOTICE1: Don't use this class with MYSQL >=2.23
use inet_aton() and inet_ntoa() instead.
See: php_and_mysql.txt
There are network addresses types in PostgreSQL too.
See: http://developer.postgresql.org/docs/postgres/datatype-net-types.html
NOTICE2: Don't use this class with PHP >=4.0
use long2ip() and ip2long() instead.
See: php_and_mysql.txt
To Store ip-addresses in a database, you will
need a field type of Char(15).
This class let's you store the ipnumbers as
an integer ( between -2139062144 and 2139062143)
This class has two functions
1) decode ip->number
2) encode number->ip
Both functions return false for empty or wrong ip-numbers
While using this class you will spare typical 11 bytes
for every ipnumber you store.
Char(15) needs 15 Bytes
SIGNED INT(10) needs 4 Bytes
Version 0.1a
Last change: 2002/09/22
copyrigth 2002 Email Communications, http://www.emailcommunications.nl/
written by Bas Jobsen (hide@address.com)
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, 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
class storeip
{
function decode(&$ip)
{
$partjes=explode('.',$ip);
$ip=0;
if(count($partjes)!=4) {$ip=false; return;}
for($x=0; $x<4; $x++)
{
if(!is_numeric($partjes[$x])||$partjes[$x]>255 || $partjes[$x]<0) {$ip=false; return;}
$ip+=$partjes[$x] << ($x*8);
}
}
function encode(&$decode_ip)
{
if(!is_numeric($decode_ip)){$decode_ip=false; return;}
$ip_string='';
for($i=0; $i<4; $i++)
{
if($i>0)$ip_string.='.';
$c=($decode_ip & (255 << ($i*8))) >> ($i*8);
if($c<0)$c+=256;
if($c<0 || $c>255){$decode_ip=false; return;}
$ip_string.=$c;
}
$decode_ip=$ip_string;
}
function get_decoded($ip)
{
$this->decode($ip);
return $ip;
}
function get_encoded($ip)
{
$this->encode($ip);
return $ip;
}
}
?>