This post explains you about how to implement php cookie ajax based rating script.

Cookie is a small file that stores on the user’s computer.Cookies is a mechanism for storing data in the remote browser and thus tracking or identifying return users.

In this script I am using cookie for user tracking.
 
Download Link   Demo Link

 

Sample database design for table name contact.

This table contains id (primary key), name and age.

CREATE TABLE IF NOT EXISTS `rating` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
  `rating` int(11) NOT NULL,
  `click` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

dbConnect.inc.php

Contains database connectivity code

$mysql_db_hostname = "Host name";
$mysql_db_user = "UserName";
$mysql_db_password = "Password";
$mysql_db_database = "Database Name";

$con = mysql_connect($mysql_db_hostname, $mysql_db_user,
       $mysql_db_password or die("Could not connect database");
//Create a new connection
mysql_select_db($mysql_db_database, $con) or
                 die("Could not select database");
// select database

index.php

Contains product display code.

include_once('inc/dbConnect.inc.php');
include_once('inc/rating.php');
//connect with database
$query="select * from rating order by id desc";
// query for fetch all products
$res=mysql_query($query);
$countRatinh=mysql_num_rows($res);
// check how many products are avaliable
//Java Script Code 
<script type="text/javascript" src="js/jquery-1.4.1.min.js">
</script>
// jquery library file
<script type="text/javascript">
function changeRating(rateId,rateNo){
   var dataString = 'rateId='+ rateId + '&rateNo=' + rateNo;
   $("#product_flash_"+id).show();
   $("#product_flash_"+id).fadeIn(400).html
                          ('<img src="image/loading.gif" />');
   $.ajax({
     type: "POST",
     url: "process.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);
           $("#product_flash_"+rateId).html('&nbsp;');
           $("#product_"+rateId).html(successMessage);
        }else{
           var errorMessage=result.substring(position+2);
           $("#product_flash_"+rateId).html(errorMessage);
        }
     }
   }
  });
}
</script>

process.php

Contains the logic part and cookie code.

include_once('inc/dbConnect.inc.php');
// connect with database
include_once('inc/rating.php');
$id=$_POST['rateId'];
$rateNo=$_POST['rateNo'];

 if(isset($_COOKIE['rating'."_".$id])) // check cookie
      echo "error||Already Rated"; // if exist display message
 else{

    $query="update rating set rating=rating+$rateNo,click=click+1
            where id='$id'";
     // update rating/clickcount
     $res=mysql_query($query);
     $query="select rating,click from rating where id='$id'";
     // check updated new rating
     $res=mysql_query($query);
     $rating=mysql_fetch_array($res);

     $expire=time()+60*60*24*30;
     setcookie("rating"."_".$id, "rating"."_".$id, $expire);
     //set cookie
    echo "success||".getRating($id,$rating['rating'],$rating['click']);
 }

 

rating.php

Contains the star rating code

<?php
function getRating($id,$rating,$click){
    $startCount= round($rating/$click);
    $blankCount=5-$startCount;
    $ratingHTML='';
    for($i=0;$i<$startCount;$i++){
       $rate=$i+1;
       $ratingHTML.='<img src="image/star.png"
                     onclick=changeRating("'.$id.'","'.$rate.'")>';
    }
    for($i=$startCount;$i<5;$i++){
       $rate=$i+1;
       $ratingHTML.='<img src="image/white.png"
                     onclick=changeRating("'.$id.'","'.$rate.'")>';
    }
  return $ratingHTML;
}
?>