PDF Generation Dependent on $_POST
I generate a PDF in my browser, using a PHP script, where the PDF-generating script depends on variables sent to it through POST. When POST is empty, PDF cannot be generated and it issues a warning saying "PDF Must be Regenerated".
Certain situations triggered POST array being empty, namely, if I hit browser's Refresh button while being on the page with PDF, for whatever reason, POST data does not get resent and there is no prompt asking me to resend it, it just becomes empty and the script detects it and the warning is issued. The warning is PDF Must be Regenerated
, which is what I put in when POST array is empty in my code.
PDF Generation Workaround Dependent on $_SESSION
So a work-around I did is to save POST data to SESSION and then use SESSION when POST is empty. Like below:
if (empty($_POST))
{
// try restore from session first
$_POST['product_count'] = $_SESSION['product_count'];
if (empty($_POST['product_count'])) // if we failed.. session is empty
{
print 'PDF Must be Regenerated';
exit();
}
}
else
{
//save POST into SESSION
$_SESSION['product_count'] = $_POST['product_count'];
}
Browser's Save feature tries to generate PDF after SESSION expires
And all was well and fine until users started noticing a certain behavior:
- They generate PDF using script
- Leave the browser idle for a while doing other business, until SESSION expires behind the scenes
- Then they use browser's Save-As feature to save the PDF.
The browser, instead of saving PDF from memory (PDF is still visible inside the browser), browser our company uses (Mozilla), makes a request to the server, asking for a fresh new copy of the PDF. But ... while doing so, POST is empty because POST is not being resent, and SESSION is empty because SESSION has expired during the idling stage.
This results in a corrupted PDF, where PDF is actually an ASCII text file containing words PDF Must be Regenerated
. Users don't notice it until it is too late.
But, Mozilla apparently makes a call to the server, PHP scripts pass the user login credentials test, but all the while having empty POST and SESSION, resulting in corrupt PDF.
Login credentials check does not depend on SESSION and to be honest I can't yet tell what it depends on because I can't easily reproduce the issue so I can't readily test.
The work-around is obviously to save PDF soon after it has been generated, before SESSION expires, but user is "always right" and I want to let the user to be able to save the PDF even when SESSION expired. My question is "how".
How? What can I do?