0

I am trying to figure out why this does not work. I am trying add and remove elements to an array that is stored in a cookie in a comma delimited fashion such as "1,2,3,4,5" and then echo the array size. With the arrangement of the php as I have it below, the array size is not updating after the elements have been added to the array when I run the php in a browser. It only updates the array the second time I run the php in a browser. Can someone please assist me with the correct php so that the php will add the elements and update the array size? Thank you.

<?
setcookie ("values", "1,2,3,4,5", time()+1000);
$aCookie = explode(",",$_COOKIE['values']);
$aCookieSize = count($aCookie);
echo $aCookieSize;
?>

1 Answer 1

1

The function setcookie is used to write the specified cookie into the headers of the server's answer. But it does not update current request informations.

When you first run the script, the client do not have any cookies. Meaning $_COOKIE is empty. setcookie include your cookie into your answer header (but does not set it for $_COOKIE). On the second run, the client have his cookie so $_COOKIE is set and setcookie is just overridding the cookie everytime.

Also, notice that php allow to send array with this form:

setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.