PHP is having a function called setcookies for setting a value for cookie variable.
The sample syntax for using this function is,
Here, username is the cookie variable (i-e $_COOKIE['username']) and $username is the value to be assigned for this cookie variable.
time()+3600 represents that this cookie variable will be expired in 1 hour.
We faced an issue when we try to change the value of cookie varibale using this function in between the php code. This situation is arised as our requirements mandated the change in cookie value after some steps in the php code. The issue is, cookie value is not updated. "echo" of the cookie variable showed only the old value.
When we did further analysis, we noticed that cookie value is set correctly only when we refresh the web page two times. From the internet search we came to know that it is the expected behaviour. As the cookie will be sent as part of the Header, it should be sent before sending any content. Since we are using setcookies in between the code it seems some content has been sent already before setting the cookie.
So to avoid this issue, just we created one dummy php file (e.g redirect.php). In the code after using setcookies we put a code for redirecting the page to this redirect.php instead of redirecting to the intended file. In this redirect.php we put a code for redirecting to the intended file.
This approach solved the issue completely without any further issues.