I am using java playframework2 and it seems that I can not get the correct ip address with it. It constantly changes after each request.
I mean traffic analytic sites implement javascript in your header to analyse your traffic, so it should be possible with javascript right?
-
If the framework doesn’t provide such a function are there other ways to get the ip from the client?
-
I want to build a visitor counter, but it should only count unique visitors. Maybe I don’t have to use the ip address?
I know that there is http://jsonip.appspot.com/ but it’s to unreliable. I couldn’t use it the last 2 days because it had exceeded its quota on google app engine.
here is my code
public static <T> void increaseViews(String title, String ip,
Class<T> objClass) throws UnknownHostException, MongoException,
DbAuthException {
Logger.info("Ip address: " + ip);
String ipCache = title + "-" + ip;
if (Cache.get(ipCache) == null) {
Cache.set(ipCache, "block", 86400);
increment(title, objClass);
}
}
private static <T> void increment(String title, Class<T> objClass)
throws UnknownHostException, MongoException, DbAuthException {
// update database
}
and I call it like this:
UserGuides.increaseViews(ug.title, request().remoteAddress(),
UserGuides.class);
If I follow the logs on Heroku, my ip address is different everytime I reload the page.
Heroku will not directly return the remote address to your play application. Instead what you are seeing is various internal load balancing servers that are part of heroku. However, the good news is that you can easily fix this as per: https://play.lighthouseapp.com/projects/82401/tickets/705-remoteaddress-seems-to-get-dynamically-client-ip-on-heroku
Simply put: add this to your application.conf