Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to use $id as part of cookie.

Featured Replies

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?

  • 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 by Lovelock

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.