Skip to content
Advertisement

Include File Get New Content Last Changed

I’ve created a file called “settings.php”

and inside settings.php

<?php 
return '123'; 
?>

And in index.php I updated the content of settings.php using file_put_contents

$config = '1234';
file_put_contents('settings.php', '<?php return ' . var_export($config), true) . ';');

After successfully updated the content, in index.php I call the settings.php

$config = include(BASEPATH.'settings.php');

But after I print_r, the result is

123

and after sometimes waiting (maybe 5 up to 15 seconds), I do refresh page again and the content changed to

1234

The problem is, why the updated content not directly changed after I refresh the page? how to get the newly updated content from settings.php after I change its content.

Advertisement

Answer

Check what cache solution is on your server thru phpinfo().

Then you can search solution.

In first you can try this if opcache solution is installed on your server:

if (function_exists('opcache_invalidate')) {
    opcache_invalidate('second.php');//Reset file cache
}

or this for normal raw files:

clearstatcache();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement