PHP is having a function called setcookies for setting a value for cookie variable.
The sample syntax for using this function is,
setcookies("username",$username,time()+3600);
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.
More Articles...
Shop at Rajamanickam.com | Birthday Gift Idea? | Hire me for $6 per Hour
Get 3 useful ebooks for Rs 99 in India and $5.99 globally
Get a 75% commission | ChatGPT and and Google Gemini for Beginners (Use Discount code QPT)
Search This Blog
Art of Talking to AI | Tech eBook | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
No comments:
Post a Comment