I have a requirement wher i need to store the country of the user in the database. I can keep a check box or combo box for that but is there any way to fetch the country info without the user having to enter it ?? Is there a way to get the country name from where user is accessing the web site?
我需要将用户的国家/地区存储在数据库中。我可以保留一个复选框或组合框,但是有没有办法获取国家信息而无需用户输入?有没有办法从用户访问网站的位置获取国家/地区名称?
2 个解决方案
#1
0
Yes, there is a way to to do this, but some work is required on your part. When a user accesses your website, you can get his/her IP address using:
是的,有一种方法可以做到这一点,但您需要做一些工作。当用户访问您的网站时,您可以使用以下方式获取他/她的IP地址:
String ip = (request.getHeader("X_FORWARDED_FOR") == null ? request.getRemoteAddr() : request.getHeader("X_FORWARDED_FOR"));
Then you can convert this ip address into its binary form:
然后你可以将这个ip地址转换成二进制形式:
long binIp = ipToLong(ip);
with ipToLong
function being something like this:
使用ipToLong函数是这样的:
public long ipToLong(String addr) {
String[] addrArray = addr.split("\\.");
return Long.parseLong(addrArray[0]) << 24 +
Long.parseLong(addrArray[1]) << 16 +
Long.parseLong(addrArray[2]) << 8 +
Long.parseLong(addrArray[3]);
}
And finally use IP address database to look up which country this value belongs to. There are several providers. We get ours from software77 - it's free - and store in a local database, updating local DB once a month.
最后使用IP地址数据库查找该值所属的国家/地区。有几个提供商。我们从software77获得我们的 - 它是免费的 - 并存储在本地数据库中,每月更新一次本地数据库。
A few notes:
几点说明:
- Sometimes the address will not be set, so you'll need to have some reasonable defaults.
- Sometimes the address will be spoofed and hence not represent a real IP address (format-wise), so you'll need to have reasonable exception handling.
- Sometimes the address will be from a reserved range (e.g. 192.168/16), so, again, you'll need to have some reasonable defaults.
有时地址不会被设置,因此您需要有一些合理的默认值。
有时地址会被欺骗,因此不代表真实的IP地址(格式化),因此您需要进行合理的异常处理。
有时地址将来自保留范围(例如192.168 / 16),因此,您需要有一些合理的默认值。
#2
0
that's the IP Locator service. See the sample code and the application @ http://www.geobytes.com/iplocator.htm
The source is in PHP though, you can try to write it in java.
这是IP定位器服务。请参阅示例代码和应用程序@ http://www.geobytes.com/iplocator.htm虽然源代码是PHP,但您可以尝试在java中编写它。
#1
0
Yes, there is a way to to do this, but some work is required on your part. When a user accesses your website, you can get his/her IP address using:
是的,有一种方法可以做到这一点,但您需要做一些工作。当用户访问您的网站时,您可以使用以下方式获取他/她的IP地址:
String ip = (request.getHeader("X_FORWARDED_FOR") == null ? request.getRemoteAddr() : request.getHeader("X_FORWARDED_FOR"));
Then you can convert this ip address into its binary form:
然后你可以将这个ip地址转换成二进制形式:
long binIp = ipToLong(ip);
with ipToLong
function being something like this:
使用ipToLong函数是这样的:
public long ipToLong(String addr) {
String[] addrArray = addr.split("\\.");
return Long.parseLong(addrArray[0]) << 24 +
Long.parseLong(addrArray[1]) << 16 +
Long.parseLong(addrArray[2]) << 8 +
Long.parseLong(addrArray[3]);
}
And finally use IP address database to look up which country this value belongs to. There are several providers. We get ours from software77 - it's free - and store in a local database, updating local DB once a month.
最后使用IP地址数据库查找该值所属的国家/地区。有几个提供商。我们从software77获得我们的 - 它是免费的 - 并存储在本地数据库中,每月更新一次本地数据库。
A few notes:
几点说明:
- Sometimes the address will not be set, so you'll need to have some reasonable defaults.
- Sometimes the address will be spoofed and hence not represent a real IP address (format-wise), so you'll need to have reasonable exception handling.
- Sometimes the address will be from a reserved range (e.g. 192.168/16), so, again, you'll need to have some reasonable defaults.
有时地址不会被设置,因此您需要有一些合理的默认值。
有时地址会被欺骗,因此不代表真实的IP地址(格式化),因此您需要进行合理的异常处理。
有时地址将来自保留范围(例如192.168 / 16),因此,您需要有一些合理的默认值。
#2
0
that's the IP Locator service. See the sample code and the application @ http://www.geobytes.com/iplocator.htm
The source is in PHP though, you can try to write it in java.
这是IP定位器服务。请参阅示例代码和应用程序@ http://www.geobytes.com/iplocator.htm虽然源代码是PHP,但您可以尝试在java中编写它。