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

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

harrysdad
Forum Newbie
Posts: 2
Joined: Sat Jul 30, 2005 9:54 pm

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

Post 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???
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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/
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Relying on an user's IP to be accurate is silly.
According to that script I am located somewhere in Africa.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

It said I was 300 km west of where I actually am. Not bad but not accurate enough.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

it found me exactally the city and country i am in
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

found me :lol:

nice class
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I haven't updated the geopcity.dat file in ages... Should explain why there are some ip's that are waaaaaaaaay off ;)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You should post that, too.

Script is no good without it. :wink:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.)
Last edited by timvw on Fri Apr 07, 2006 2:47 am, edited 1 time in total.
harrysdad
Forum Newbie
Posts: 2
Joined: Sat Jul 30, 2005 9:54 pm

Post 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!!!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I've combined the script with google maps ;)

http://timvw.madoka.be/comment.php?message_id=211
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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 ;)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.. ;)
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

Post by lostinphp »

gud job!!! it found me too!
bravo!
AngusL
Forum Contributor
Posts: 155
Joined: Fri Aug 20, 2004 4:28 am
Location: Falkirk, Scotland

Post 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!
Post Reply