ArizonaJohn Posted May 15, 2009 Share Posted May 15, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/158320-blocking-an-ajax-action-by-ip-address-for-24-hours/ Share on other sites More sharing options...
gregor171 Posted May 16, 2009 Share Posted May 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158320-blocking-an-ajax-action-by-ip-address-for-24-hours/#findComment-835390 Share on other sites More sharing options...
ArizonaJohn Posted May 18, 2009 Author Share Posted May 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158320-blocking-an-ajax-action-by-ip-address-for-24-hours/#findComment-836719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.