note 61723 added to reserved.variables
| From: | nospam at joot dot com | Date: | Fri, 10 Feb 2006 11:02:55 +0000 |
| Subject: | note 61723 added to reserved.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103428@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 66.207.199.35
Probable Submitter: 209.121.101.232
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61723
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61723&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61723&report=yes
Search -- http://master.php.net/manage/user-notes.php