Only allow 3 votes a week

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
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Only allow 3 votes a week

Post by xionhack »

Hello. I have a form, where you can vote for different songs. What I want is for you to only be allowed to vote 3 times a week, the voting system being reset every monday at 12:01 am. How can I do that? Thanks!
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Only allow 3 votes a week

Post by Grizzzzzzzzzz »

You could use cookies, but they can easily be got around by deleting them.

You could use I.P tracking with a SQL database, which is slightly better, but people could still get around it.

Probably the best way is to only allow people to vote if they have a registered account with a unique verified email. But that limits your voting base and excludes people that don't want to bother signing up.




It depends what you're using it for. If its simply a school project and integrity isn't taken into account, use cookies. If it's for a larger project, i'd personally use an I.P tracking system as it would be good enough. If the votes really matter and you don't want one person getting around your system and voting multiple times, use a registration system, or even a combination of all 3.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Only allow 3 votes a week

Post by xionhack »

Yes, Im sorry I didnt explain better. They have to be registered to vote. I have a table where I record the votes, who did it, and when was done. My problem is how to know when you've already voted 3 times this week.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Only allow 3 votes a week

Post by Grizzzzzzzzzz »

have another table in the db with their username, and their votes

then where the voting takes place, do an SQL statement like this

Code: Select all

 
$query= "SELECT * FROM votes WHERE name = '$name'";
$result=mysql_query($query);
$rows = mysql_num_rows($result);
 
if ($rows >= 3)
{
     //don't allow vote
}
else
{
    //do allow vote
}
 
 

then at the start of the new week you could run a php script that'll drop all the contents of the table, allowing them to start fresh.

or you could also store the week number using the php date function to do it automatically for you if its a new week.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Only allow 3 votes a week

Post by xionhack »

What if I want the week to start on monday 12:01 am and ending on sunday at 12:00
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Only allow 3 votes a week

Post by Grizzzzzzzzzz »

Then make it so it does, that link i gave you shows you to store those date in exactly that format

check the day of the week would be using

Code: Select all

 
$day = date("l");
 
check if its past a certain time would be using

Code: Select all

 
$time = date("Hi");
 
etc..
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Only allow 3 votes a week

Post by daedalus__ »

record their username and a timestamp with their vote

then create a query that says

"select blah from votes where date is older than now() but newer than 12:01am last monday"

you could figure out the day of the week sort of like this:

Code: Select all

 
$day_of_the_week = date('N', time());
 
$last_monday = mktime(0, 0, 0, date('n', time()), date('j', time()) - ($day_of_the_week - 1), date('Y', time()));
 
i didn't test that code to make sure it works but the idea is to take the iso 8601 day of the week and count your way back to last monday
Post Reply