Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
David Jarrin
Courses Plus Student 11,182 Pointslocating a visitors IP address and then location with that information.
Problem: Trying to assign a location to a visitor of a site.
Ok I have been looking around for solutions to this and this is the best code I could find.
<?php
if (getenv('HTTP_X_FORWARDED_FOR')) {
$pipaddress = getenv('HTTP_X_FORWARDED_FOR');
$ipaddress = getenv('REMOTE_ADDR');
echo "Your Proxy IP address is : ".$pipaddress. "(via $ipaddress)" ;
} else {df
$ipaddress = getenv('REMOTE_ADDR');
echo "Your IP address is : $ipaddress";
}
$country =getenv('GEOIP_COUNTRY_NAME');
$state = getenv('GEOIP_STATE_NAME');
$city = getenv('GET_CITY_NAME');
echo "Your country is $country";
echo "Your city is $state";
?>
From what I can the code is basically using the getenv method to obtain the ip address. Next I tried to assign a "state" variable a state value using getenv method (the same for city and country). I read that the GEOIP server you have to pay for or something? I'm a little lost as you may be able to tell so any push in the right direction would be appreciated.
2 Answers
Kevin Korte
28,149 PointsDisclaimer: I haven't done this myself yet.
However, I watched this video a few days ago, and thought it might be helpful to you.
https://www.youtube.com/watch?v=Rr36E8NAORI&list=UUpOIUW62tnJTtpWFABxWZ8g
James Barnett
39,199 PointsCheck out geoplugin.net
This code works
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
echo "It appears that you are connecting from "
. $geoplugin['geoplugin_city'] . ", "
. $geoplugin['geoplugin_region'] . ", "
. $geoplugin['geoplugin_countryName'];
I tested over at http://phpcodepad.com/
Ken Alger
Treehouse TeacherJames;
Great resource! Thanks for the link.
Ken
David Jarrin
Courses Plus Student 11,182 PointsDavid Jarrin
Courses Plus Student 11,182 PointsThanks man this worked great. I'll try your solution tomorrow James! Tons of help!