6
PHP caching is basically a cache created from your site to prevent it from being wasted searching for data with each page request. When a user accesses a page, is saved a copy of it in HTML on the server, this copy is valid at a given time, after that time is called the original page and created a new copy.
In theory, the application of this practice makes the site faster, I researched on the subject and found some examples, follow what I liked most:
<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
include($cachefile);
exit;
}
ob_start();
The above code is responsible for checking if you have already cached that page, if it exists then it is called, if it does not exist, the script keeps being run I have its default content.
<?php
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush();
Already in this code, the cache file is created and opened and called to the browser.
It is recommended to use this method?
In several cases we will have an area of login, and each user will have access to different parts, when saving this cache the user would not end up taking data from the other (displaying the other’s cache page)?
If yes, how to avoid?
And how to use this technique correctly (in case the one I put here is wrong)?
If the page can be accessed by all users and is the same page without modifications I believe CDN could use Cloudflare or Incapsula (the latter promises a better performance in these cases) or can use Sucuri. They are paid solutions, but they are not so expensive (Cloudflare has a free plan, but I do not know if it offers this feature), in the paid alternatives we have Sucuri for ~10 dollars, Cloudflare for ~20 dollars and Incapsula for ~40 dollars, unless mistaken. It’s just a suggestion beyond the question! Dynamic data (comments) can be loaded by JS. ;)
– Inkeliz