Page 1 of 1
Vote once?
Posted: Thu Jan 24, 2008 4:19 am
by Sindarin
I am trying to make my own voting system with simple fwrite, fread, I have everything planned well, but I can find out how I can prevent the same user from voting again?
Re: Vote once?
Posted: Thu Jan 24, 2008 4:37 am
by Agirre
There are some ways to prevent multiple voting. Best thing here, I think, is just simple IP matches. Create a file with all IPs that voted. Then compare the current users IP with the IP list to check if they already voted.
Code: Select all
/**
* Assume each IP is on a newline
*/
$iplist = file_get_contents(<ipfile>);
$iplist = explode("\n", $iplist);
if(in_array($_SERVER['REMOTE_ADDR'], $iplist)) {
// already voted
}
else {
// handle vote
file_put_contents(<ipfile>, $_SERVER['REMOTE_ADDR'], FILE_APPEND);//put IP in our file
}
Something like that would work, I use file_..._contents here because it's shorter then fread etc. (PHP5 only). And you can't check 100% if an user already voted (dynamic IPs).
Re: Vote once?
Posted: Thu Jan 24, 2008 5:40 am
by Sindarin
ah I can't use file_put_contents()
I am trying to get the ip written to the text file and then add a newline but something's wrong, the error is:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
The code I am using is:
Code: Select all
//store ip - write file
$vote_ip=fopen("ip.txt","a");
fwrite($vote_ip, "$_SERVER['REMOTE_ADDR']");
fwrite($vote_ip, "\n");
fclose($vote_ip);
Re: Vote once?
Posted: Thu Jan 24, 2008 5:50 am
by Agirre
You can't double quote $_SERVER. And you could add the newline in one statement, like:
Code: Select all
...
fwrite($vote_ip, $_SERVER['REMOTE_ADDR'] . "\n");
...
Re: Vote once?
Posted: Thu Jan 24, 2008 5:59 am
by Sindarin
Thanks it works perfectly!
So there isn't any dynamic-ip/proxy proof solution? (not that I really care though that much)
Re: Vote once?
Posted: Thu Jan 24, 2008 6:44 am
by Agirre
Sindarin wrote:Thanks it works perfectly!
So there isn't any dynamic-ip/proxy proof solution? (not that I really care though that much)
You could include cookies, which are more solid, but they can be deleted by the user. So no, as far as I know, there is no such solution. For example, you can't recognize a person who voted on his own pc and then voted on a friends pc. It's impossible to find a perfectly solid solution.
Re: Vote once?
Posted: Thu Jan 24, 2008 7:56 am
by Zoxive
The easiest way I can think of on the spot is if you had User Accounts on your website.
Then a simple check in the database to see if that user voted or not.
Re: Vote once?
Posted: Thu Jan 24, 2008 5:26 pm
by Jonah Bron
The only drawback, is if the user has a dynamic IP connection. Another method that would fix that, would be to have a cookie that is set to expire after voting is over.