HELP! Trying to use php to identify the city a user is in
Moderator: General Moderators
HELP! Trying to use php to identify the city a user is in
I am writing a PHP app and I want to be able to identify the city a user is login in from. (ie if someone from Indianapolis visits the site, I want to display a welcome message which says something like "Welcome Indianapolis User".
Any ideas???
Any ideas???
timvw wrote something back in Sept of last year that might be of use to you:
here it is in action
http://timvw.madoka.be/ip2location/
Code: Select all
<?php
// +---------------------------------------------------------------------------
// | ip2location.php
// |
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net
// | Update: 2004-09-23 02:00
// |
// | Lookup the GIS information of the visitor and display it on a map.
// +---------------------------------------------------------------------------
$background = "earth.jpg";
$color = array(255, 0, 0);
$addr = $_SERVER['REMOTE_ADDR'];
// lookup geoip record
require_once("geoipcity.inc");
$gi = geoip_open("GeoIPCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$addr);
// load backgroundimage
$im = imagecreatefromjpeg($background);
$color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
// get image sizes
$width = imagesx($im);
$height = imagesy($im);
// calculate location on the image
$posx = round(($record->longitude + 180) * ($width / 360));
$posy = round((($record->latitude * -1) + 90) * ($height / 180));
// compose message
$string = "$record->city, $record->country_name";
// draw on the background image
imagefilledrectangle($im, $posx - 2, $posy - 2, $posx + 2, $posy + 2, $color);
imageline($im, 0, $posy, $width, $posy, $color);
imageline($im, $posx, 0, $posx, $height, $color);
imagestring($im, 3, $posx + 5, $posy + 5, $string, $color);
// output image
header("Content-Type: image/png");
imagepng($im);
// clean up
imagedestroy($im);
geoip_close($gi);
?>http://timvw.madoka.be/ip2location/
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
Well, the path in the code is GeoIPCity.dat , so the chance that exists is real 
(Meaby that a little websearch provides more recent files.)
(Meaby that a little websearch provides more recent files.)
Last edited by timvw on Fri Apr 07, 2006 2:47 am, edited 1 time in total.
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
timvw wrote:I've combined the script with google maps
http://timvw.madoka.be/comment.php?message_id=211
Good job, I've been playing around with the google maps API lately, if I ever finish, I'll post it