PHP caching is basically a cache created from your site to prevent it from processing data by fetching data for each page request. When a user accesses a page, a copy of it is saved in HTML on the server, this copy is valid at a certain time, after that time the original page is called and a new copy is created.
In theory, the application of this practice makes the site faster, I researched the subject and found some examples, follow what I liked the 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 the page is already cached, if it is then called it, if it does not exist, the script is still running I have its default content.
<?php
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush();
In this code, the cache file is created and opened and called to the browser.
Is it recommended to use this method?
In several cases we will have a login area, and each user will have access to different parts, when saving this cache the user would not end up getting data from the other one (displaying another's cache page)?
If yes, how to avoid?
And how to correctly use this technique (if I put it here is wrong)?