[BNM] Setting cookie name using a variable (PHP)

Simon Willison simon at simonwillison.net
Thu Nov 1 09:58:07 GMT 2007


On 1 Nov 2007, at 09:45, delarge wrote:

> setcookie('$itemName', $like, $inTwoMonths);
>
> But it doesn't work - it actually calls the cookie '$itemName'
> Tried without the apostrophes as well but no joy

Without the apostrophes should work fine (with them you're telling it  
to use a literal string):

setcookie($itemName, $like, $inTwoMonths);

If it's not working, I suggest adding some debugging code in:

setcookie($itemName, $like, $inTwoMonths);
echo "Attempted to set a cookie called $itemName containing $like";

Make sure there's no output being sent to the browser before your  
call to setcookie().


More information about the BNMlist mailing list. Powered by Wessex Networks