Jump to content

strtotime to check if they voted within the last hour


3raser

Recommended Posts

My site allows users to vote once per hour, and it checks the next time they vote if it's been 3600 seconds  (1 hour) since their last vote. But, it always returns the value 0.

 

			$grab = mysql_fetch_assoc($has_voted);
			$calc = strtotime($grab['date']) - strtotime(date("M-d-Y"));

			if($calc > 3600)
			{
				mysql_query("DELETE FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'");
				header("location:votes.php?id=". $id ."");
			}
			else
			{
				echo 
				echo "You've voted within the last hour. Please wait ". $calc ." seconds.";
			}

Link to comment
Share on other sites

I tried time(), but it just lets users vote as much as they want. o.O

 

                         if(mysql_num_rows($has_voted) > 0) 
		{
			$has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'");
			$grab = mysql_fetch_assoc($has_voted);

			$calc = $grab['date'] - time();

			if($calc > 3600)
			{
				mysql_query("DELETE FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'");
				header("location:vote.php?id=". $id ."");
			}
			else
			{
				echo "You've voted within the last hour. Please wait ". $calc ." seconds.";
			}

		}
		else
		{
			if($_POST['name_vote_f'] && $_POST['id_vote_f'])
			{
				echo "You have successfully voted!";
				mysql_query("INSERT INTO votes VALUES (null, '". $_SERVER['REMOTE_ADDR'] ."', '". time() ."')");
				mysql_query("UPDATE sites SET votes = votes + 1 WHERE id = '$id'");
			}
			else
			{
					$grab = mysql_fetch_assoc($query);

					echo "<b>Voting for ". $grab['title'] .". Are you sure?</b><br/><br/>
					<form action='vote.php' method='POST'>
					<input type='hidden' name='name_vote_f' value='". $grab['title'] ."'>
					<input type='hidden' name='id_vote_f' value='". $id ."'>
					<input type='submit' value='Yes, vote now!'>
					</form>";
			}
		}

Link to comment
Share on other sites

Again, what does $grab['date'] contain.

 

Also, you want to subtract the old value ($grab['date']) from the current time. When you do it your way, you will get a negative number which will always be less than 3600. :-)

 

Ken

Link to comment
Share on other sites

Again, what does $grab['date'] contain.

 

Also, you want to subtract the old value ($grab['date']) from the current time. When you do it your way, you will get a negative number which will always be less than 3600. :-)

 

Ken

 

Thanks! And $grab['date'] is just time().

 

EDIT: Still no difference. :(

Link to comment
Share on other sites

Do some debugging on your own. Put echo statements in to show what values are being used:

<?php
$has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'");
$grab = mysql_fetch_assoc($has_voted);
        $cur = $time();
        echo "Current time: $cur<br>\n";
        $saved = $grab['date'];
        echo "Saved time: $saved<br>\n";
        $diff = $cur - $saved;
        echo "Diff: $diff<br>\n";
?>

 

This should show where the problem lies.

 

Ken

 

Link to comment
Share on other sites

Do some debugging on your own. Put echo statements in to show what values are being used:

<?php
$has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'");
$grab = mysql_fetch_assoc($has_voted);
        $cur = $time();
        echo "Current time: $cur<br>\n";
        $saved = $grab['date'];
        echo "Saved time: $saved<br>\n";
        $diff = $cur - $saved;
        echo "Diff: $diff<br>\n";
?>

 

This should show where the problem lies.

 

Ken

 

Thanks ken, I figured it out. It seems $has_voted was in the wrong place.

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.