note 61723 deleted from reserved.variables by philip

From: Date: Wed, 21 Feb 2007 16:00:38 +0000
Subject: note 61723 deleted from reserved.variables by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-123644@lists.php.net to get a copy of this message
Note Submitter: nospam at joot dot com ---- [Editor's note: As much as you may hate us lazy programmers, we highly recommend the use of DOM (http://php.net/dom) or SimpleXML (http://php.net/simplexml) for handling XML data. It's much, much, much, much safer than your method. :)] Man, I hate lazy programmers. Let's have no more excuses for requiring your viewers to fill in their City and Country, okay? PART ONE - The Lookup $ip = get_remote_ip(); $location = http_get( "http://api.hostip.info/?ip=$ip" ); $contents = get_tag_contents( $location, "Hostip" ); $city = trim( get_tag_contents( $contents, "gml:name" ) ); $country = trim( get_tag_contents( $contents, "countryAbbrev" ) ); if( stristr( $city, "private" ) ) { $city = ""; } if( stristr( $country, "xx" ) ) { $country = "US"; } PART TWO - The Functions function get_remote_ip() { return $_SERVER['REMOTE_ADDR']; } function http_get( $url ) { $request = fopen( $url, "rb" ); $result = ""; while( !feof( $request ) ) { $result .= fread( $request, 8192 ); } fclose( $request ); return $result; } function get_tag_contents( $xml, $tag ) { $result = ""; $s_tag = "<$tag>"; $s_offs = strpos( $xml, $s_tag ); // If we found a starting offset, then look for the end-tag. // if( $s_offs ) { $e_tag = "</$tag>"; $e_offs = strpos( $xml, $e_tag, $s_offs ); // If we have both tags, then dig out the contents. // if( $e_offs ) { $result = substr( $xml, $s_offs + strlen( $s_tag ), $e_offs - $s_offs - strlen( $e_tag ) + 1 ); } } return $result; } PART THREE - The HTML Form <input type="text" name="city" id="city" value="<? echo $city; ?>" size="40" maxlength="50" /> <option value="AU" <? if( $country == "AU" ) echo "SELECTED='true'" ?>>Australia</option> <option value="CA" <? if( $country == "CA" ) echo "SELECTED='true'" ?>>Canada</option> <option value="GB" <? if( $country == "GB" ) echo "SELECTED='true'" ?>>United Kingdom</option> <option value="US" <? if( $country == "US" ) echo "SELECTED='true'" ?>>United States</option> etc.

« previous php.notes (#123644) next »