<?php
// class.ahostlookup.php
// version 1.1.0, 8 August 2005
//
// 8 August 2005: Added a hit counter to this class. The class can now count the
// number of hits from each host. It will also record the time and date of
// the last hit from each host. Existing scripts using the original database for
// this class should still work with the updated database. However, to use this
// version of the class, the database must be updated.
//
// License
//
// PHP class to lookup an IP address and return the host name for that IP address,
// the upper and lower IP address for that block of IP's, and the ZIP code for the host.
// Also, it will now keep a count of the hits for each host and the date and time
// of each host's last hit.
//
// Copyright (C) 2005 George A. Clarke, hide@address.com, http://gaclarke.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 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.
//
// 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.
//
// **********************************************************************
// A typical use of this class would be to insert the following code in your index
// page:
// include "class.ahostlookup.php";
// $ip=$REMOTE_HOST;
// $hst= new ahostlookup();
// ****** edit next line to put your own username, password and database name.
// $hst->SetUserPassDb("Username","Password","DatabaseName");
// if(!$rslt=$hst->GetHost($ip)){
// $rslt=$hst->GetArin($ip,true);
// }
//
// Then the results in the array $rslt can be displayed if desired.
class ahostlookup{
var $user; //mySQL username
var $pass; //mySQL password
var $dbase; //mySQL database name
var $ip; //ip address (xxx.xxx.xxx.xxx)
var $low; //low ip address (xxx.xxx.xxx.xxx)
var $high; //high ip address (xxx.xxx.xxx.xxx)
var $host; //isp or host name (result of lookup)
var $zip; //zip code of isp (5 digit zip code)
//only 5 digit numerical zip codes are saved
var $count;
var $date;
var $time;
// Set the MySQL username, password, and database name.
function SetUserPassDb($user,$pass,$dbase)
{
$this->user=$user;
$this->pass=$pass;
$this->dbase=$dbase;
}
// This function looks up the ip address ($ip) in the MySQL database and
// increments 'count' and saves the date and time.
// If not found, it returns false.
// If found, it returns an array of ['host'],['lower'],['upper'],['zip'],['count']
// ['host'] = Host name or ISP name to whom this IP address is registered.
// ['lower'] = The lower IP address of this block of IP's.
// ['upper'] = The upper IP address of this block of IP's.
// ['zip'] = The ZIP code of the Host or ISP of this IP address.
// ['count'] = The number of hits from the Host.
// ['date'] = Date of the last hit from the host.
// ['time'] = Time of day of the last hit from the host.
function GetHost($ip)
{
$this->ip=ip2long($ip);
$db = mysql_connect("localhost", $this->user,$this->pass);
mysql_select_db($this->dbase,$db);
$sql = "SELECT * FROM isps WHERE $this->ip BETWEEN lower AND upper";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
if ($id=$myrow['id']){
$this->host = $myrow['host'];
$this->low = long2ip($myrow['lower']);
$this->high = long2ip($myrow['upper']);
$this->zip = $myrow['zip'];
$this->count = $myrow['count']+1;
$this->date=date("m/d/Y");
$this->time=date("g:i A");
$upd="UPDATE isps SET count='$this->count',date='$this->date',time='$this->time' WHERE id='$id'";
$result=mysql_query($upd);
$ret['host']=$myrow['host'];
$ret['lower']=$this->low;
$ret['upper']=$this->high;
$ret['zip']=$myrow['zip'];
$ret['count']=$this->count;
$ret['date']=$this->date;
$ret['time']=$this->time;
mysql_close($db);
return $ret;
}else{
return false;
}
}
// This function looks up the ip address ($ip) from the ARIN website.
// If not found, it returns false.
// If found, it returns an array of ['host'],['lower'],['upper'],['zip'],
// ['count'],['date'],['time']
// ['host'] = Host name or ISP name to whom this IP address is registered.
// ['lower'] = The lower IP address of this block of IP's.
// ['upper'] = The upper IP address of this block of IP's.
// ['zip'] = The ZIP code of the Host or ISP of this IP address.
// ['count'] = The number of hits from the Host.
// ['date'] = Date of the last hit from the host.
// ['time'] = Time of day of the last hit from the host.
// The URL used for this ARIN lookup is http://ws.arin.net/cgi-bin/whois.pl?queryinput=$ipaddr
// where $ipaddr is the IP address that was passed to the function.
// The results from this inquiry is then parsed to extract the returned information.
// There is probably a better way to extract the information but this works.
// The $save paramater that is passed is either true of false.
// This determines if the results from the ARIN search is saved to the database or not.
// If it is saved, it will be found in the database the next time.
// If the IP address is not found in ARIN, false will be returned.
function GetArin($ip,$save)
{
$ipaddr=$ip;
$string = file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput=$ipaddr");
if(!strstr($string,"No match")){
$I1 = strpos($string,$ipaddr);
$host=substr($string,$I1);
if (!strpos($host,"OrgName:")) {
$I2 = strpos($host,"HREF=");
$link = substr($host,$I2+6);
$I2 = strpos($link,"\"");
$link = "http://ws.arin.net".substr($link,0,$I2);
$string = file_get_contents($link);
}
$I1 = strpos($string,"OrgName:")+8;
$I2 = strpos($string,"OrgID:");
$LW1 = strpos($string,"NetRange:");
$range = substr($string,$LW1);
$LW1 = strpos($range,">")+1;
$LW2 = strpos($range,"</");
$H1 = strpos($range,">",$LW2+4)+1;
$H2 = strpos($range,"</",$LW2+4);
$L=$I2-$I1-1;
$this->host=trim(substr($string,$I1,$L));
$L=$LW2-$LW1;
$this->low = substr($range,$LW1,$L);
$L=$H2-$H1;
$this->high = substr($range,$H1,$L);
$I1=strpos($string,"PostalCode:")+12;
$this->zip=substr($string,$I1,5);
if(!ctype_digit($this->zip)){
$this->zip="";
}
if($save){
$db = mysql_connect("localhost", $this->user,$this->pass);
mysql_select_db($this->dbase,$db);
$ilower=ip2long($this->low);
$iupper=ip2long($this->high);
$this->date=date("n/m/Y");
$this->time=date("g:i A");
$queryinsert = "INSERT INTO isps (host,lower,upper,zip,count,date,time) VALUES ('$this->host','$ilower','$iupper','$this->zip',1,'$this->date','$this->time')";
$resultinsert = mysql_query($queryinsert) or die("Query failed:error was ".mysql_error()."<br>".$this->host.":".long2ip($ilower).":".long2ip($iupper).":".$this->zip);
mysql_close($db);
}
$ret['host']=$this->host;
$ret['lower']=$this->low;
$ret['upper']=$this->high;
$ret['zip']=$this->zip;
$ret['count']=1;
$ret['date']=$this->date;
$ret['time']=$this->time;
return $ret;
}else{
return false;
}
}
}
// End of Class
?>