Cookie based Like Dislike Voting System in PHP, Mysql

Abhigyan Singh 12th Jul 2020

This tutorial will learn about cookie based like dislike counter in php, ajax, mysql. Voting system become very important part in website development to rate their product, article etc. This voting system is similar to youtube like dislike. Here also we have two thumb up and down for like and dislike.

If user clicks on up thumb then one like counter will increase and a count increase in table and same as if user clicks on down thumb then one dislike counter will increase and a count increase in table.

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.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk