cookie - only vote once
Ive got a webpage where users input a story/event and other users can like or dislike the post.
I want to create a cookie that allows users to only vote once per post (each page will have many posts)
I know that storing in a database would be a better way incase the user delete his/her cookies but the post will not be of high importance. Also i would imagine that creating a database for cookies would take a lot of code and the databas开发者_开发知识库e would be huge as there will be mass amount of posts and likes for each.
I think creating a cookie would be a better option for what i need
I have no idea how to do this though and ive searched high and low on google but i dont know if im searching wrong key words or its just my lack of knowledge letting me down
So could someone please advise me how to create this cookie for what i need please
Thanks
The modern replacement for cookies is localStorage
. That's basically a variable of which the data will be saved on the client's computer, so you can access it at any time, even when the user has navigated or rebooted his computer.
It's rather straight-forward, e.g. you could do:
function vote() {
if(localStorage['votedForPost' + postid] == 'true') return; // abort if already voted
// ...
localStorage['votedForPost' + postid] = 'true'; // you can only store strings
}
The only drawback is that it's not available in all browsers, especially older ones which are still used such as IE7.
If you want to know how to manipulate cookies, here is a good explanation with code.
I use the jQuery "cookies" plug-in for my website.
精彩评论