I'm using a 5 stars AJAX voting sistem. When an article is rated, a cookie is added:
Name has_voted_40
and value 40
, where 40 is the unique id of the article.
I want to check if the user has rated at least one article. How could I "search" through cookies to verify if at least one article has been rated?
For example, if a cookie of this form is found, the variable $voted
to be set to 1
.
The only idea I have:
<?
$voted=0;
for($i=1;$i<500;$i++) {
if(isset($_COOKIE["has_voted_".$i]))
$voted=1;
}
?>
I set the limit to 500, but the id can be bigger than 500 and I think it's not the best idea.
you only want to know if a rating has been done? Why not make a variable "hasVoted" that would not be set normally, and on all votes just either make it 1, or if it exists then add one.
Now if no "hasVoted" exists, the user has not voted and otherwise it has. And you even get the number of votes for (almost) free like this :D