1. Computing

A Simple Rating System in PHP

By , About.com Guide

3 of 3

Processing Votes
This code should be placed below the database connection, but above the rest of the script.
 //We only run this code if the user has just clicked a voting link
 if ( $mode=="vote") 
 { 
 
 //If the user has already voted on the particular thing, we do not allow them to vote again 	$cookie = "Mysite$id"; 
 	if(isset($_COOKIE[$cookie])) 
 		{ 
 		Echo "Sorry You have already ranked that site <p>"; 
 		} 
 
 //Otherwise, we set a cooking telling us they have now voted 
 	else 
 		{ 
 		$month = 2592000 + time(); 
 		setcookie(Mysite.$id, Voted, $month); 

 		 //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating 
 mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id"); 
 		Echo "Your vote has been cast <p>"; 
 		} 
 } 
 
The voting links we created in the previous step simply sent us back to the same page passing along the site ID we wanted to vote for and our rating for it. This new code only runs if the person has clicked one of those links, otherwise it is ignored.

In order to hinder people spamming the rankings, we place a cookie in their browser before we update the database. If we detect this cookie is already there, the script will not let the user vote again. Otherwise, the information is updated. If your site's users need to register to be able to rank, a better way to prevent multiple votes would be to store this information right in the database and not as a cookie.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Step By Steps
  5. Rating Script - PHP Rating Script - Rate Script PHP

©2013 About.com. All rights reserved.