January 1, 201313 yr Hello, have tried every combo i can but cant get it working. Current code i used for testing: <?php include 'connect.php'; if( isset( $_COOKIE['VoteCookie'] ) ) { echo "Voted Already"; header('Refresh: 4; URL=../index.php'); exit; } else { $Month = 2592000 + time(); //this adds 30 days to the current time setcookie(VoteCookie, date("F jS - g:i a"), $Month); echo "cookie sent"; $qry=mysql_query("SELECT * FROM products LIMIT 0, 10"); if(!$qry) { die("Query Failed: ". mysql_error()); } if ($_GET["voteup"] == '+') { mysql_query('UPDATE products SET rating = rating +1 WHERE id = "' . $_GET["id"]. '"'); echo $_GET["voteup"]; echo $_GET["id"]; echo $_GET["rating"]; header("Location: ../index.php"); } else { if ($_GET["votedown"] == '-') { mysql_query('UPDATE products SET rating = rating -1 WHERE id = "' . $_GET["id"]. '"'); echo $_GET["votedown"]; echo $_GET["id"]; echo $_GET["rating"]; header("Location: ../index.php"); } } } ?> But this ofcourse sets a cookie for every item. I want it like cookie name is VoteCookie$id. So it get the id from the button, checks if it has one, if not it does and then continues to process the vote. If there is one then it echos an error. Any tips on using $_GET in the cookie name?
January 1, 201313 yr setcookie('VoteCookie'.$_GET['id'], date("F jS - g:i a"), $Month); also sanitise your variables before using them in a sql query!
January 2, 201313 yr Author doesnt work im afraid. Am checking the cookies that are saved and its still just showing VoteCookie, no id. Saying that, cookies are now not setting. Uhhhhh :/ Edited January 2, 201313 yr by Lovelock
January 2, 201313 yr Rather than storing tens/hundreds of individual cookies on someone's machine, it would be preferable to store an array of IDs within a single cookie. Here's a procedural script I've just written up which should achieve that for you. You can put your own code into the 'if' statements at the bottom. You should consider having a cookie handler class if you're using them a lot. Also +1 to D4Y0 - you should sanitise your inputs! On top of that mysql_query is deprecated - stop using it! <?php function get_vote_array(){ $cookie = isset($_COOKIE['VoteCookie']) ? json_decode($_COOKIE['VoteCookie']) : null; $arr = is_object($cookie) && isset($cookie->{'voted_for'}) ? $cookie->{'voted_for'} : null; return $arr; } function has_voted_for($id){ $arr = get_vote_array(); if (is_array($arr)){ return in_array($id, $arr); } return false; } function set_voted_for($id){ $arr = get_vote_array(); if (is_array($arr)){ $arr[] = $id; setcookie('VoteCookie', json_encode(array('voted_for' => $arr)), strtotime("+1 month")); } else { setcookie('VoteCookie', json_encode(array('voted_for' => array($id))), strtotime("+1 month")); } } if (isset($_GET['id'])){ if (!has_voted_for($_GET['id'])){ //They haven't yet voted for this item set_voted_for($_GET['id']); //Do stuff here echo "Not in this array (until page refresh):<br />"; print_r(get_vote_array()); } else { //They have already voted for this item echo "Already in this array:<br />"; print_r(get_vote_array()); } } ?>
Create an account or sign in to comment