0

I am trying to make a log of users heading to my website. I want to save their IP addresses, as well as city and country info.

Thanks in Advance.

Matt
  • 22,721
  • 17
  • 71
  • 112
yusufiqbalpk
  • 282
  • 1
  • 6
  • 18

4 Answers4

2

Try using MaxMind GeoIP database which I find the best solution that's freely available. It has a set of methods to retrieve the city and the country by IP and to get the ip just use: $_SERVER['REMOTE_ADDR']

Ignas
  • 1,965
  • 2
  • 17
  • 44
1

You can get the IP from $_SERVER['REMOTE_ADDR']. To get location you will have to find some service that provides lookup and check the IP you just collected.

You might also consider signing up for Google analytics.

danneth
  • 2,721
  • 1
  • 23
  • 31
  • I'm not suggesting you do this manually. You take the IP collected and send it inside your PHP script to some webservice/api/library and then store that response together with the IP. But personally, I would only use Google Analytics, but that depends on your needs of course – danneth Apr 26 '12 at 12:57
  • Sorry for that. I think that using google analytics is a better idea then holding a long and long database for ip or relying on other apis and libs that can take while in an operation. Thanks for your comment. You answered first and right. Thanks – yusufiqbalpk Apr 26 '12 at 13:00
  • @yusufiqbalpk: No worries, I could have been clearer there. As for the downvote, I don't know... – danneth Apr 26 '12 at 14:29
1

I Think you should go for Google analytic, All these data you will get from there. Otherwise for ip address you can use this script

function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;
}

& for country name

http://geoip.wtanaka.com/cc/$ipaddr

this will return country ISO code.

Sumant
  • 954
  • 1
  • 18
  • 39
0

Do the users input their City and Country? If yes then easily save the data to database.

Or do you want to track these by IP? If this one - download/buy the database of the countries by ip from http://www.ip2country.net/download.html and only put the IP into the database.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78