I have a need to save $_COOKIE["foo"]
in order to process it after request finish.
The example code is next:
echo $_COOKIE["foo"]; // all good, cookie shown
fastcgi_finish_request();
$fp = fopen('/tmp/POINTINIT', 'w');
fwrite($fp, $_COOKIE["foo"]); // nothing is written
fclose($fp);
Same thing happens with next examples:
// Saving it to another variable + adding data
echo $_COOKIE["foo"]; // all good, cookie shown
$foo = $_COOKIE["foo"] . " 1";
fastcgi_finish_request();
$fp = fopen('/tmp/POINTINIT', 'w');
fwrite($fp, $foo); // " 1" is written to the file
fclose($fp);
I am doing this in an existent server which I didn't configure and I'm not very good with the whole PHP and PHP-FPM ecosystem.
How does that happen and can I somehow turn global variable scoping?
Thanks.
My Answer:
variable values after calling fastcgi_finish_request() in PHP-FPM, but I noticed that the variables are getting cleared for unknown reasons. This behavior is unexpected and can be frustrating when trying to persist data across requests.
One possible explanation for this issue is that fastcgi_finish_request() is designed to finish the request and send the response to the client, which may involve clearing certain variables to free up memory and resources. This behavior is intended to improve performance and prevent memory leaks.
To work around this issue, you can try saving the $_COOKIE variable values to a persistent storage such as a database or session before calling fastcgi_finish_request(). This way, you can retrieve the values in the next request and avoid losing them.
Alternatively, you can try using other methods to pass data between requests, such as using session variables or passing data in the response headers. These methods may be more reliable for persisting data across requests in PHP-FPM.
Overall, it's important to understand the behavior of fastcgi_finish_request() and consider alternative approaches for persisting data if needed. By carefully managing your data and understanding the limitations of PHP-FPM, you can avoid unexpected issues and ensure a smooth user experience.
Rate this post
4 of 5 based on 3942 votesComments