Shawn31313
Shawn31313

Reputation: 6052

Allowing only one vote per person on a voting system

I'm working on a little posting system so I can post posts on my site and people can like and dislike it.

It looks like this:

the voting system

At the moment you can upvote and downvote as many times as you would like. I know how to make the images not clickable with JavaScript but I also need a way to do this in PHP because someone could just make the buttons clickable again with fireBug or the Chrome Console.

This is probable the first thing i'm actually making in PHP so i'm still a beginner. Thanks for any suggestions.

Upvotes: 3

Views: 9438

Answers (3)

Tim Withers
Tim Withers

Reputation: 12059

I am not going to just write code for you, and there are probably dozens of workable examples on script sites. Here are a few tips to get you pointed in the right direction:

Session variables - $_SESSION[] - Check if it is set, and then set them after a vote. As long as they don't close the browser, they won't be able to vote again.

Cookies - $_COOKIE[] - Same as session, but can remain even if they close and open their browser again.

IP Address - $_SERVER['REMOTE_ADDR'] - Keep a record in a MySQL table of IPs and votes.

Login system - Only allow authenticated users to vote, and then keep track of the votes in the database.

Any combination of the above is acceptable. Hope that gets you pointed in the right direction.

Upvotes: 9

nickb
nickb

Reputation: 59699

Since you're going to learn this, I'm not going to post any complete code. I can give an overview, though.

The best way to do this is to store votes in a database table (probably MySQL):

| vote_id | user_id | post_id | vote |

Where:

  • vote_id is an auto-increment column that creates a unique ID for each vote
  • user_id is an identifier of who the user is that submitted this vote
  • post_id is an identifier for the post the user is voting on
  • vote determines whether this vote was up or down.

Now, you can form queries to determine whether or not somebody has already voted on the post, and act accordingly.

Upvotes: 6

Kalpesh
Kalpesh

Reputation: 5685

You need to validate it on the server-side i.e. in PHP code. You can do that either by IP address (if non-logged in user / guest) or by username (for logged in user)

There is no way you can stop users by client-side validation.

Upvotes: 0

Related Questions