PDA

View Full Version : IP to country script..



markman641
11-13-2011, 11:31 PM
ok i have the following:


<?php
/*#####################################################
#
#
#
# IPGEO PARSE
#
# v 1.0
#
# By: John Calabrese
# (xchapter7x)
#
# a million thanks to the people at
# hostip.info
# for providing free access to information
#
################################################x7x##*/
class geoReturn
{
function geoReturn()
{
$this->ipadd='';
$this->city='';
$this->state='';
$this->country_code='';
$this->country_name='';
$this->longitude='';
$this->latitude='';
}
}
class ipGeo
{
function ipGeo()
{
//nothing to do
}
function getGeo($ipadd)
{
$this->xml_parser = xml_parser_create();
$this->rss_parser = new IPGEO_PARSE();
xml_set_object($this->xml_parser,$this->rss_parser);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
$res = new geoReturn();
$fp = fopen("http://api.hostip.info/?ip=".$ipadd,"r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
{
xml_parse($this->xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
}
$res->city = $this->rss_parser->city;
$res->state = $this->rss_parser->state;
$res->country_code = $this->rss_parser->country_code;
$res->country_name = $this->rss_parser->country_name;
$res->longitude = $this->rss_parser->longitude;
$res->latitude = $this->rss_parser->latitude;
fclose($fp);
xml_parser_free($this->xml_parser);
return $res;
}
}
class IPGEO_PARSE {

function IPGEO_PARSE()
{
$this->gml_name = 0;
$this->countryName = 0;
$this->countryAbbrev = 0;
$this->gml_coordinates = 0;
$this->hostip = 0;
$this->city = '';
$this->state = '';
$this->country_name = '';
$this->country_code = '';
$this->longitude = '';
$this->latitude = '';
$this->noresult = 0;
}

function startElement($parser, $tagName, $attrs) {
if($tagName == strtoupper("Hostip")){
$this->hostip = 1;
$this->noresult=0;
}elseif($tagName == strtoupper("gml:name")){
$this->gml_name = 1;
}elseif($tagName == strtoupper("countryName")){
$this->countryName = 1;
}elseif($tagName == strtoupper("countryAbbrev")){
$this->countryAbbrev = 1;
}elseif($tagName == strtoupper("gml:coordinates")){
$this->gml_coordinates = 1;
}
}

function endElement($parser, $tagName) {
if($tagName == strtoupper("Hostip")){
$this->hostip = 0;
}elseif($tagName == strtoupper("gml:name")){
$this->gml_name = 0;
}elseif($tagName == strtoupper("countryName")){
$this->countryName = 0;
}elseif($tagName == strtoupper("countryAbbrev")){
$this->countryAbbrev = 0;
}elseif($tagName == strtoupper("gml:coordinates")){
$this->gml_coordinates = 0;
}
}

function characterData($parser, $data) {
if($this->hostip){
if($this->gml_name){
$info = explode(",",$data);
if(isset($info[1])){
$this->city = $info[0];
$this->state = str_replace(" ","",$info[1]);
}else{
$this->state = '';
$this->city = '';
$this->noresult = 1;
}
}elseif($this->countryName){
if($this->noresult){
$this->country_name = '';
}else{
$this->country_name = $data;

}
}elseif($this->countryAbbrev){
if($this->noresult){
$this->country_code = '';
}else{
$this->country_code = $data;
}
}elseif($this->gml_coordinates){
$info = explode(",",$data);
$this->longitude = $info[0];
$this->latitude = $info[1];
}
}
}
}
?>



and the display page is this:


<?php
include("ipGeo.php");
$ipadd = '64.233.187.99';
$geo = new ipGeo();
$res = $geo->getGeo($ipadd);
print $res->country_name."\n";
print $res->country_code."\n";
?>

What im trying to do is GET IT TO WORK!! As that script looks now it takes the $ipadd and puts it into the ipgeo.php script. I had it before where i made it:


<?php
include("ipGeo.php");
$ip = $_SERVER['REMOTE_ADDR'];
$ipadd = $ip;
$geo = new ipGeo();
$res = $geo->getGeo($ipadd);
print $res->country_name."\n";
print $res->country_code."\n";
?>

and that seemed to work, except it only showed up for ME. For everyone else it showed up as a blank page.. help? or do you have another script like this that will take the IP and tell me what country they are from? thanks