Vote once?
Moderator: General Moderators
Vote once?
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?
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.
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).
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
}
Re: Vote once?
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:
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);
Last edited by Sindarin on Thu Jan 24, 2008 6:18 am, edited 2 times in total.
Re: Vote once?
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?
Thanks it works perfectly!
So there isn't any dynamic-ip/proxy proof solution? (not that I really care though that much)
So there isn't any dynamic-ip/proxy proof solution? (not that I really care though that much)
Re: Vote once?
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.Sindarin wrote: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?
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.
Then a simple check in the database to see if that user voted or not.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Vote once?
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.