php - My poll has a 'back button' loophole -
have had couple questions answered nicely here , i've got more trouble can with:
i have sql database holds poll question answer , user ip address. here (now working!) php code:
// check see if user has voted $current_user = $_server['remote_addr']; $select_query = "select * w_poll_counter user_ip = '" . $current_user ."';"; $result = mysql_query($select_query); if($result) { $row = mysql_fetch_array($result); $user_from_db = $row['user_ip']; if($current_user === $user_from_db) { //user voted - show results page header("location: scripts/show_results.php"); exit(); } }
the code works great, except there's 1 problem... after user votes , sees results page, can click browser's 'back' button , vote again, since code check ip address doesn't run in instance.
what need fix issue?
thanks!
check if user has voted before executing update statement.
also should take better care, script vulnerable sql injections. https://stackoverflow.com/a/60496/3595565
i can show example of implementation via pdo:
$pdo = new pdo('mysql:host=localhost;dbname=test;charset=utf8;', 'dbuser', 'dbpassword'); $stmtcheck = $pdo->prepare("select * w_poll_counter user_ip = ?"); $stmtcheck->execute(array($_server['remote_addr'])); $result = $stmtcheck->fetchall(pdo::fetch_assoc); if(count($result) === 0){ //update }
Comments
Post a Comment