Page 1 of 2

HELP! Trying to use php to identify the city a user is in

Posted: Sat Jul 30, 2005 10:00 pm
by harrysdad
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???

Posted: Sat Jul 30, 2005 10:10 pm
by Burrito
timvw wrote something back in Sept of last year that might be of use to you:

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);
?>
here it is in action
http://timvw.madoka.be/ip2location/

Posted: Sat Jul 30, 2005 11:55 pm
by John Cartwright
Relying on an user's IP to be accurate is silly.
According to that script I am located somewhere in Africa.

Posted: Sun Jul 31, 2005 2:42 am
by m3mn0n
It said I was 300 km west of where I actually am. Not bad but not accurate enough.

Posted: Sun Jul 31, 2005 4:37 am
by shiznatix
it found me exactally the city and country i am in

Posted: Sun Jul 31, 2005 5:54 am
by malcolmboston
found me :lol:

nice class

Posted: Sun Jul 31, 2005 6:54 am
by timvw
I haven't updated the geopcity.dat file in ages... Should explain why there are some ip's that are waaaaaaaaay off ;)

Posted: Sun Jul 31, 2005 7:56 am
by m3mn0n
You should post that, too.

Script is no good without it. :wink:

Posted: Sun Jul 31, 2005 8:48 am
by timvw
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.)

Posted: Sun Jul 31, 2005 6:39 pm
by harrysdad
Thanks Guys. It finds me and found people in several other cities as well. Must keep an up to date file for accuracy though. Thanks Again!!!

Posted: Mon Aug 22, 2005 6:04 pm
by timvw
I've combined the script with google maps ;)

http://timvw.madoka.be/comment.php?message_id=211

Posted: Mon Aug 22, 2005 10:09 pm
by d3ad1ysp0rk
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 ;)

Posted: Tue Aug 23, 2005 5:39 am
by timvw
I've a seen a couple of "social network" diagrams that analyzed the messages in an irc channel, and then drawed diagrams to show the links between the people...

I think some people would find it cute if you combined the diagram if you draw the users on a map with their respective location.. ;)

Posted: Tue Aug 23, 2005 5:41 am
by lostinphp
gud job!!! it found me too!
bravo!

Posted: Wed Aug 24, 2005 6:15 pm
by AngusL
Got it right when I was in Edinburgh, but in Falkirk it now reckons I'm somewhere in Bradford. :mrgreen: Good try though!