Reputation: 19
I have created a polling system for my institute which uses the client's IP-address to identify unique voters. I have used $_SERVER['REMOTE_ADDR']
.
The problem is that the institute uses a LAN and hence all users have same global IP. So, only one user is able to vote.
How to get the local IP of the voting person?
Here is the code snippet I have used :
if(!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
Upvotes: 0
Views: 1919
Reputation: 373
It is not possible to get the local IPs of computers behind a shared connection. And you can make another tip to identify unique voters: 1- Send a cookie to voter machine with key and Unique value add this to Global IP
$_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']
2- You can add another factor, like the user agent string (Browser) that tends to differ And operating system version or gets the standard host name for the local machine.
Upvotes: 0
Reputation: 1275
In case of IPv4 most client addresses are masked behind NAT, on your server side you ONLY see the globally routable address which is the router's own global address.
In case of IPv6 the local address for all intents and purposes will be the same as the global one, so you'll find that in $_SERVER['REMOTE_ADDR']
.
That being said, I'd also like to caution you against using the X-Forwarded-For
header for ANYTHING unless it comes from a trusted source (e.g. your own reverse proxy). The client can set this header to an arbitrary value and can cause some funny or even dangerous bugs to be triggered.
On a practical note I'd like to add that using the IP address to limit how many times one can vote is a somewhat broken practice since I rent at the moment a block of 16 IP addresses and I know people who can get their hands on a full C-sized block (255 addresses) and you'll be blocking lots of people behind provider NAT's and such. In case of IPv6 everyone will have billions of addresses anyway, so the whole concept of IP blocking will be a lot more broken.
I recommend you tie the voting to something a bit more stable like phone number or e-mail registration if possible.
Upvotes: 2
Reputation: 3549
In the case of NAT, you cannot get the internal IP in your server side code.
Upvotes: 2