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.

 

 <?php
          if($totalCount > 0){
             while($getResult=mysql_fetch_array($rec)){
               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>';
             }
          }else{
               echo 'Opsss... No Result Found.';
          }
          ?>




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

 

2. getResult.php

 

<?php
include 'dbConfig.php';

$type=$_POST['type'];
$id=$_POST['id'];

if(isset($_COOKIE['rateLikeChnage'."_".$id]))
    echo "error|| You Already Voted";
else{
    if($type=='like'){
       $fieldName='like_count';
    }elseif($type=='dislike'){
       $fieldName='dislike_count';
    }else{
       die();
    }
    $query="update  rate set $fieldName=$fieldName+1 where id='$id'";
    $res=mysql_query($query);
    
    $query="select $fieldName from  rate where id='$id'";
    $res=mysql_query($query);
    $result=mysql_fetch_array($res);
    $count=$result[$fieldName];
    
    $expire=time()+60*60*24*30;
    setcookie("rateLikeChnage"."_".$id, "rateLikeChnage"."_".$id, $expire);
    echo "success||".$count;
}
?>

3. dbConfig.php

 

<?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 discussdesk@gmail.com

"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.

Comments