Jump to content

Recommended Posts

Hello,

 

The code below works great.  I'm using it to add a vote to an ID in an entry in a MySQL database.  However, I would like to make it so that once a unique vote is cast, then the same vote can not be re-cast from a given IP address for 24 hours.  I define a unique vote as having two variables:  $_SESSION['find'] as the table name, and $id as the row in that table. 

 

So basically, if a given IP address is used to vote for row $id in table $_SESSION['find'], I want to block that same IP address from casting the same vote for 24 hours. 

 

Any ideas how I can do this?

 

Thanks in advance,

 

John

 

 

 

<head>

<?php
session_start();
?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Voting Practice</title>

</head>
<body>

<?php
mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
mysql_select_db("sand2") or die(mysql_error());

function getAllVotes($id)
{
/**
Returns an array whose first element is votes_up and the second one is votes_down
**/
$votes = array();
$q = "SELECT * FROM {$_SESSION['find']} WHERE id = $id";
$r = mysql_query($q);
if(mysql_num_rows($r)==1)//id found in the table
	{
	$row = mysql_fetch_assoc($r);
	$votes[0] = $row['votes_up'];
	$votes[1] = $row['votes_down'];
	}
return $votes;
}

function getEffectiveVotes($id)
{
/**
Returns an integer
**/
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1];
return $effectiveVote;
}

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

//get the current votes
$cur_votes = getAllVotes($id);

//ok, now update the votes

if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1;
$q = "UPDATE {$_SESSION['find']} SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down') //voting down
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE {$_SESSION['find']} SET votes_down = $votes_down WHERE id = $id";
}

$r = mysql_query($q);
if($r) //voting done
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote;
}
elseif(!$r) //voting failed
{
echo "Failed!";
}


?>

</body>
</html>

Server side with some response wit text: You have already voted today.

Try somehow to separate code from design (html) and do logics on client JavaScript.

you basically do not need head and body tags for ajax response. Actually this can cause an error in some browsers.

Just make Ajax response with simple div tag, containing the results from voting.

Hi gregor171,

 

I don't want to display a message that says anything. 

 

The code I attached is triggered by a hyperlink that uses Ajax on another page.  Right now, when the user casts a vote on the other page, the hyperlink goes away until the browser is refreshed.  I would like to make it so that the hyperlink is still gone even after the browser is refreshed, but only for 24 hours.  I was hoping to use the PHP command $_SERVER['REMOTE_ADDR'] to look up the user's IP address, and then attach some sort of timer to it that would not let the same vote be cast again for 24 hours. 

 

I'm just not sure how to go about doing this. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.