Created
February 17, 2018 20:14
-
-
Save codigoconjuan/25e07184d1b432d7df693eb2e65c9985 to your computer and use it in GitHub Desktop.
Cachear páginas PHP a HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Definir un nombre para cachear | |
$archivo = basename($_SERVER['PHP_SELF']); | |
$pagina = str_replace(".php", "", $archivo); | |
// Definir archivo para cachear (puede ser .php también) | |
$archivoCache = 'cache/'.$pagina.'.html'; | |
// Cuanto tiempo deberá estar este archivo almacenado | |
$tiempo = 36000; | |
// Checar que el archivo exista, el tiempo sea el adecuado y muestralo | |
if (file_exists($archivoCache) && time() - $tiempo < filemtime($archivoCache)) { | |
include($archivoCache); | |
exit; | |
} | |
// Si el archivo no existe, o el tiempo de cacheo ya se venció genera uno nuevo | |
ob_start(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment