PHP Cookie not saved after foreach
Get the solution ↓↓↓Solution:
Cookies can only be set BEFORE OUTPUT... therefore if you are outputting in your foreach loop, cookies can not be set afterwards.
However, I believe you can actually use output buffers to work around this problem... So the following should work just fine...
ob_start();
setcookie(...);
ob_end_flush();
Answer
Solution:
Finally solved with this code:
<?php
ob_start();
foreach ($values as $value){
// CODE
echo $value;
}
setcookie('test', 'This is a test', time() + 3600 , '/', '.mydomain.com');
ob_end_flush();
if(isset($_COOKIE['test'])){
echo 'The cookie is ' . $_COOKIE['test'];
} else {
echo 'No cookie has been set';
}
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.