2

i have a voting system that you dont have to login to and i want to know how i can set it to where they can vote once per day, either by blocking IP or something of the sort. i prefer not to have a database

<script type="text/javascript">

var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}

document.write('<a href="#" onclick="linkClick()">Vote Now!</a>');

$('#clicked').parent().bind('click', function(evt) {
$(this).unbind('click');   
/* do long time task....

evt.preventDefault();
});


</script>

You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times

this code as it stands will only let a use vote once and only once, i would like it to be every 24 hours and in html, thank you in advance

Mason
  • 81
  • 2
  • 8
  • 1
    I think you're better off using some server-side scripting, such as PHP. Anyone could break into the HTML and vote as many times as they wanted otherwise. – JCOC611 Feb 06 '11 at 03:07

2 Answers2

3

It's not possible to implement this in a safe way in javascript or HTML. The reason is that as a web-user I can clear my cookies, open a new browser, or make calls to your server programatically without having to use your user interface. This means you need something on the server to prevent me from actually submitting twice within a day, regardless of what the web-page looks like.

My recommendation would be to use a user-login to verify that the user hasn't posted twice.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
  • user-login good, a unique computer ID hash also good idea, MAC address sure, but ip-address can get pretty hairy with practically everybody using NAT. – P.Brian.Mackey Feb 06 '11 at 03:31
  • That's true, I like your idea of using other unique identifiers, I think either way you run into the issue of people either using other computers or new logins. At some point you can't actually tell if one user is different from another, and that's something you have to accept :) – Pan Thomakos Feb 06 '11 at 03:40
0

You could create a cookie and check that each day to see if they have voted already. Although JCO611 is correct, this is easily bypassed. I would recommend using some server side code.

For more on cookies in javascript: http://www.quirksmode.org/js/cookies.html

Roloc
  • 1,910
  • 2
  • 13
  • 16
  • thank you two :D im gonna use cookies in the mean time while i get my login system perfected :) – Mason Feb 06 '11 at 03:47
  • Not sure why this didn't get voted up. The OP asked how to do it with Javascript. I gave the answer and cautioned him about the issues of doing it that way. – Roloc Feb 06 '11 at 06:51