Cookie based Like Dislike Voting System in PHP, Mysql

Here we implement cookie method to track the user uniqueness. so that one user can clicks only one time. if he tries to vote next time , he will get message that you already voted. So this system tracks that a user can vote only one time. For every user a unique ID is generated and store on cookie. If he again try then we will check whether corresponding cookie exist or not.

Here we have three file

1. index.php

2. getResult.php

3. dbConfig.php


1. index.php


In this file you will voting content like your product name, thumb image, and like dislike counter. If user clicks on any of the thumb in this file then the file getResult.php will be called by Ajax. We will send product ID, like dislike type etc by this Ajax to this file.

Then we will check whether the cookie exists or not if not then we will update table with corresponding column and set a new cookie.


          if($totalCount > 0){
               echo '<div class="list">';
                      echo '<div class="rate_name">'.$getResult['rate_name'].'</div>';
                      echo '<div class="rate_id" id="rate_id_'.$getResult['id'].'">&nbsp;</div>';
                      echo '<div class="rate_change"><img src="image/like.png" onclick=rateChange("like","'.$getResult['id'].'")> <span id="rate_change_'.$getResult['id'].'">'.$getResult['like_count'].'</span></div>';
                      echo '<div class="rate_change_like"><img src="image/dislike.png" onclick=rateChange("dislike","'.$getResult['id'].'")> <span id="rate_change_like_'.$getResult['id'].'">'.$getResult['dislike_count'].'</span></div>';

               echo '</div>';
               echo 'Opsss... No Result Found.';

<script type="text/javascript">
function rateChange(type,id){
      var dataString = 'id='+ id + '&type=' + type;
      $("#rate_id_"+id).fadeIn(400).html('<img src="image/loading.gif" />');
      type: "POST",
      url: "getResult.php",
      data: dataString,
      cache: false,
      success: function(result){
                    var position=result.indexOf("||");
                    var warningMessage=result.substring(0,position);
                         var successMessage=result.substring(position+2);
                         var errorMessage=result.substring(position+2);


2. getResult.php


include 'dbConfig.php';


    echo "error|| You Already Voted";
    $query="update  rate set $fieldName=$fieldName+1 where id='$id'";
    $query="select $fieldName from  rate where id='$id'";
    setcookie("rateLikeChnage"."_".$id, "rateLikeChnage"."_".$id, $expire);
    echo "success||".$count;

3. dbConfig.php


define ("DB_HOST", ""); // set database host
define ("DB_USER", ""); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME",""); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

Here, You can set variables.

I hope this tutorial will help you. If you have any query regarding this, Please feel free to ask in comment section.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.