Jump to content

Using a Cookie to Block an Ajax Action for 1 hour


ArizonaJohn

Recommended Posts

Hello,

 

Below I have the code for two PHP files that work together.  They work great.  The page tsearch12.php makes an Ajax request to votes12.php to add a vote to a row in a table. 

 

I would like to use a cookie to limit a unique vote to once per hour per user.  I define a unique vote as voting for a row in a table.  I would like users to be able to still vote for other rows during the hour after the original row has been voted for.

 

I have started a cookie using PHP, but I'm not sure if it is set up right.  Here is the cookie:

 

$Voteblock = 360 + time();
$Value = ($_SESSION['find'], $id);
setcookie(Votelimit, $Value, $Voteblock);

 

$_SESSION['find'] is the table name, and $id is the row.  I'm not sure what value to use for the cookie.

 

Then, I'm not sure what action to enforce if the cookie is live.  Here is what I have so far:

if(isset($_COOKIE['Votelimit']))
{ 
$last = $_COOKIE['Votelimit']; 
//action?
} 
else 
{ 
//action?
} 

 

I would like the voting link to just not appear if the cookie is live.  But I'm not sure how to do that.  Any ideas?

 

The code for my two pages is below.

 

Thanks in advance,

 

John

 

 

 

 

 

tsearch12.php:

 

<head>

<?php
session_start();
$find = strip_tags($find);
$find = trim ($find);
$_SESSION['find'] = $find;
?>

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

<link rel="stylesheet" type="text/css" href="tablestyles.css"> 

<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
$("a.cell1").click(function(){
//get the id
the_id = $(this).attr('id');

// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");

//fadeout the vote-count 
$("span#votes_count"+the_id).fadeOut("fast");

//the main ajax request
	$.ajax({
		type: "POST",
		data: "action=vote_up&id="+$(this).attr("id"),
		url: "votes12.php",
		success: function(msg)
		{
			$("span#votes_count"+the_id).html(msg);
			//fadein the vote count
			$("span#votes_count"+the_id).fadeIn();
			//remove the spinner
			$("span#button"+the_id).remove();
		}
	});
});

$("a.vote_down").click(function(){
//get the id
the_id = $(this).attr('id');

// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");

//the main ajax request
	$.ajax({
		type: "POST",
		data: "action=vote_down&id="+$(this).attr("id"),
		url: "votes12.php",
		success: function(msg)
		{
			$("span#votes_count"+the_id).fadeOut();
			$("span#votes_count"+the_id).html(msg);
			$("span#votes_count"+the_id).fadeIn();
			$("span#button"+the_id).remove();
		}
	});
});
});	
</script>



</head>

<body>

<div class="line"></div>

<div class="smalllogo"><a href="index.php"><img src="images/newlogo.gif" alt="Name" width="246" height="56" border="0"/></a></div>

<div class="line2"></div>







<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{


//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
unset($_SESSION['find']);	

}

// Otherwise we connect to our Database
mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
mysql_select_db("sand2") or die(mysql_error());

// We preform a bit of filtering


if(isset($_COOKIE['Votelimit']))
{ 
$last = $_COOKIE['Votelimit']; 
//action?
} 
else 
{ 
//action?
} 



$find = strip_tags($find);
$find = trim ($find);



$result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
or die(mysql_error());


if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){
print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC");


print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){

$effective_vote = $row['votes_up'] - $row['votes_down']; 

print "<tr>";

print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";
}
else{
print "None found";
}





//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
unset($_SESSION['find']);	

}


}
?> 


</body>
</html>

 

 

Votes12.php:

 

<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!";
}


$Voteblock = 360 + time();
$Value = ($_SESSION['find'], $id);
setcookie(Votelimit, $Value, $Voteblock);

?>

</body>
</html>

Link to comment
Share on other sites

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.