0

I have run into an interesting situation.

There is a QR Code that gets generated from a PHP functionality (phpqrcode/qrlib.php - for those who are aware of it - SO thread phpqrcode QR Code Resize when displayed directly in browser)

The beauty of that functionality is that when a user opens the php page in a browser (like example.com/qr-page.php), it perfectly displays the QR code as an image.

The way to use it on any other webpage is to "source" that webpage as an image in the standard HTML <img> tage, like <img src='qr-page.php' /> It perfectly displays the QR code as an image.

However, the issue is that sometimes the dynamically generated QR code gets INCORRECT input, so it fails to generate the QR code image. It then displays the standard "Missing Image" icon (like follows):

enter image description here

Still OK till here.

But then, the user CORRECTs the input, & possibly because of image caching, the standard "Missing Image" icon continues to display even after several refreshes of the webpage with the correct inputs.

Then, if the user visits the same webpage on another device, the QR code image gets displayed correctly. Therefore, it seems to be a caching issue.

On the PHP page, I've tried to use the following to prevent caching:

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

But the problem persists.

Any idea how to get this working please?

The code: show-qr-code.php (this calls the image)

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Set the text content which will be used to generate the QR code in qr-page.php
"$text4qrcode = "abc";
// Now include the image sourced as php page
echo "<img src='qr-page.php?a=".rand(100,9999)."' alt='Scan missing ' />";
// other code

qr-page.php (this creates the QR code image)

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Include the qrlib file
include './phpqrcode/qrlib.php';
QRcode::png($text4qrcode); // this generates the image of QR code using $text4qrcode variable value
ADyson
  • 57,178
  • 14
  • 51
  • 63
levent001
  • 174
  • 7
  • 1
    If you have written the HTML that contains the `img` tag have you appended a querystrig to the src, such as `src="qr-page.php?i==time();?>"` etc – Professor Abronsius Apr 14 '23 at 06:25
  • How is the "input" passed anyway? It would appear to make sense as query parameters to `qr-page.php`, but that doesn't seem to be the case here…!? – deceze Apr 14 '23 at 06:39
  • The input is passed "passively" like in php file include - it is set as a variable in the calling file - like "$text4qrcode = "abc"; - and then this variable value automatically gets used in the qr-page.php – levent001 Apr 14 '23 at 06:43
  • updated the Q with necessary info & code. – levent001 Apr 14 '23 at 06:49
  • Did you try to generate the image a new? Because a image will not be loaded if the src has not changed. And changing something to the same value, well, it does not count. Or you try with a simple query string. – Dxg125 Apr 14 '23 at 06:50
  • @Dxg125 - Not sure if I get you correctly. If I simply replace QRcode::png($text4qrcode); with QRcode::png('abc'); and open the qr-page.php in the browser, it displays the QR code perfectly. – levent001 Apr 14 '23 at 06:52
  • Not quite, i meant changing the literal src="page.php" to "page.php" won't do anything at all. But yes, that resolves it probably. Just to be clear, if you hardcode the value (and switch the value in code, as the user would do by resubmitting), will it be getting cached? If not, it cannot be a chache problem. Also try on resumbitting accessing qr-page.php?count=1, then ?count=2 and so on. this will resolve issues with the cache. – Dxg125 Apr 14 '23 at 07:30
  • Thanks @Dxg125 - tried adding that argument, but it doesnt help. It continues to shows the no image error icon. – levent001 Apr 14 '23 at 07:54
  • _"tried adding that argument, but it doesnt help"_ - did you _change_ the value of the parameter each time? (If you keep it the same, then this obviously won't work as a "cache buster", because it would still be the _same_ URL then.) – CBroe Apr 14 '23 at 07:58
  • Yes @CBroe - changed it each time by random generation rand function of PHP Scan missing – levent001 Apr 14 '23 at 08:08
  • 1
    The cache control headers would need to be in `qr-page.php` because its the response to the request to that file which is (probably) being cached, not `show-qr-code.php` – ADyson Apr 14 '23 at 08:15
  • 1
    Unless your random values happened to be the same in two consecutive showings of that image, your browser would have no other choice than to request that _new_ URL. (And you should be able to verify what actually happens via your browser dev tools network panel.) Maybe the input value is still wrong, so it causes an error again? (I still can't tell from your description, where that value is actually supposed to come from. _"it is set as a variable in the calling file"_ - what "calling" file?) – CBroe Apr 14 '23 at 08:16
  • @ADyson - have those headers in the qr-page.php file as well. It ain't helping. – levent001 Apr 14 '23 at 08:24
  • @CBroe - [1] what "calling" file? Its the "show-qr-code.php" file which has the code to the QR-generating php file. Hence I mentioned it as the "calling file". [2] Also did echo the random value generated & passed to the img src, so am sure that it is indeed different each time. – levent001 Apr 14 '23 at 08:28
  • `have those headers in the qr-page.php file as well`...oh ok, you didn't show that. Can you see them being delivered to the browser in the request the browser makes to load the image? (You can look in your network tool) – ADyson Apr 14 '23 at 08:34
  • 1
    `"show-qr-code.php" file which has the code to the QR-generating php file. Hence I mentioned it as the "calling file"`...but show-qr-code isn't calling the qr-page.php file. It's simply outputting some HTML to the browser. The _browser_ then makes a _separate_ HTTP request to qr-page.php, in which any variables you had in show-qr-code.php do not exist and are not relevant. You seem to have a bit of a basic misunderstanding of how the process works. – ADyson Apr 14 '23 at 08:37
  • Thanks again @ADyson - [1] The browser IS MAKING the request. Verified it in browser tools (PLUS the QR Code is correctly showing whenever a new input is given, PLUS it is also correctly showing when the same repeat input is given on a NEW BROWSER/DEVICE). [2] That should answer your 2nd comment abt variables not existing. I am also echoing the $text4qrcode before & after the image load, & it shows perfectly. [3] If I comment the QR code generation code & echo the $text4qrcode in the qr-page.php, there also it echoes perfectly. – levent001 Apr 14 '23 at 08:42
  • `I am also echoing the $text4qrcode before & after the image load, & it shows perfectly.`...sure, but that's in `show-qr-code.php`. That value of `$text4qrcode` does not exist in the entirely separate request the browser subsequently sends to `qr-page.php`. Variables don't persist between HTTP requests. If the line `QRcode::png($text4qrcode);` in qr-page.php is correctly receiving a variable called `$text4qrcode` (which so far we have no direct evidence for as far as I can see), it must be getting it some other way (e.g. perhaps it's defined in qrlib.php, which is included into qr-page.php). – ADyson Apr 14 '23 at 08:53
  • `The browser IS MAKING the request`...ok, so does the response show that the browser received the cache headers you've set? – ADyson Apr 14 '23 at 08:54
  • @ADyson- When the QR code is indeed displayed (on new browser), I am scanning that QR code to check what value it carries. And it does carry the correct value I am setting in that $text4qrcode variable. It means that the value is indeed getting there correctly. – levent001 Apr 14 '23 at 08:56
  • Well it must be getting it some other way, because it's literally physically impossible that it got it from anything you've set in `show-qr-code.php`, based on the code provided. That file is not linked in any way with qr-page.php (at least from what we can see). Do you perhaps also save that value somewhere, in your database or something, which qr-page (or files included into that) could retrieve it from, perhaps? I'm just guessing, but I can't see how else what you're saying could make any sense. – ADyson Apr 14 '23 at 09:00
  • 1
    Let me be frank, just as @ADyson says, something must be off. For the sake of time, would you mind to echo the variable directly as GET: echo "Scan missing "; and fill that in in QR-Page: QRcode::png($_GET['text4qrcode']);, if that works it's probably that the variable is written in some weird way that is seems like it's gettin cached – Dxg125 Apr 14 '23 at 09:03
  • 2
    Thanks again Dxg125 & ADyson. Outcome of some more trial & error attempts: [1] I switched to using SESSION variable to pass on the $text4qrcode value, and now it is working perfectly in the same browser even after initial no-image-found icon initially. No-image-found icon displayed on wrong input, & then correct QR code displayed when input corrected. [2] As suggested, by Dxg, I then temporarily attempted the GET method, and it was back to same problem - it displays no image with incorrect input, & then remains at the same no-image-found icon even after the input is corrected. – levent001 Apr 14 '23 at 09:11

0 Answers0