whats the best way of preventing someone from voting twice? How do i get the users ip address? What if they are on a large network? will everyone on that network show the sa
If users are behind NAT router or proxy server, you will see them with the same IP address. Therefore, it is not the best way of allowing users to vote once.
An alternative would be to use cookies, but again, it is possible to erase the cookie and vote again.
In your JSP, use request.getRemoteAddr(). This returns the IP address of the agent that sent the request as a String.
Also, request.getRemoteHost() will attempt to get the fully qualified host name. If it can't resolve the name however, the IP address will be returned as in getRemoteAddr().
You can get the IP address with request.getRemoteAddr(). If the network is using a NAT router, all users will get the same address though
In your JSP page simply use this expression: ${pageContext.request.remoteAddr}
AFAIK the ONLY way of preventing a second vote is by authenticating. Obviously this is not always possible, so you have to mitigate the possibility of a single user casting a ton of votes.
Referer
and User-Agent
.To get the IP of a client behind a router/firewall you can use
request.getHeader("X-FORWARDED-FOR")
.
The X-Forwarded-For (XFF) HTTP header is a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer. http://en.wikipedia.org/wiki/X-Forwarded-For
Keep in mind though, that this value can be changed by the proxies between you and the client. Though it should be the correct IP.