0

I have a script that creates a new json file every time I upload a new version of my app.

The problem is that browsers and mobile apps seems to cache that json file.

I use file_put_contents to create that json file.

Is there a way to disable cache specifically for that json file when someone read it from outside?

EDIT1: Answer the question assuming I cannot change my clients

2 Answers 2

1

Pseudo code,

Original JSON file:

{
  "param":"value",
  "param1" "value",
  ... etc..
}

JSON.PHP file

<?php 

$thatMuch = (60 * 60 * 24); // 24hrs
$cacheTime = (time() + $thatMuch);

$json = file_get_contents($originalJsonFile);

header('Pragma: public');
header('Cache-Control: maxage='.$thatMuch);
header('Expires: ' . gmdate('D, d M Y H:i:s', $cacheTime) . ' GMT');
header('Content-Type: application/json; charset=utf-8');
echo($json); 
exit;

If You output that JSON as it is, than file_get_contents is ok, otherwise, You do what You need to do.. use json_encode(), change last header directive to something else, etc.

Also, You could *(if You haven't done so, already) remove .php extension lookup via .htaccess so that .php is not needed in path, therefore, the original path/url will remain exact the same.

Something like this *(root dir .htaccess)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]
</IfModule>

And this would be enough I guess...

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting solution. I'm gonna give it a shot.
0

How do You serve that file to the endusers? Are You serving that file directly? If You do, try to serve that JSON file via/by PHP as well, with proper headers before output, where filename.json.php will take place instead of filename.json or just as Paul proposed, much easier.

2 Comments

Directly. I grant the apps with the url on login.
If You can, try what Paul has proposed, otherwise You need to force the browsers to re-fetch file again. Create a new .json.php file, than inside that file use file_get_contents to retrieve original file, than set proper headers for caching or no caching at all and for mime/file type and output it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.