Vote once?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Vote once?

Post 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?
User avatar
Agirre
Forum Newbie
Posts: 7
Joined: Wed Jan 23, 2008 1:05 pm
Location: Amsterdam, The Netherlands

Re: Vote once?

Post 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).
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Vote once?

Post 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);
Last edited by Sindarin on Thu Jan 24, 2008 6:18 am, edited 2 times in total.
User avatar
Agirre
Forum Newbie
Posts: 7
Joined: Wed Jan 23, 2008 1:05 pm
Location: Amsterdam, The Netherlands

Re: Vote once?

Post 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");
...
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Vote once?

Post by Sindarin »

Thanks it works perfectly!

So there isn't any dynamic-ip/proxy proof solution? (not that I really care though that much)
User avatar
Agirre
Forum Newbie
Posts: 7
Joined: Wed Jan 23, 2008 1:05 pm
Location: Amsterdam, The Netherlands

Re: Vote once?

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Vote once?

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Vote once?

Post 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.
Post Reply