Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 31, 2023 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/778adf68228b5762a9715765b81e60d3 to your computer and use it in GitHub Desktop.
Save code-boxx/778adf68228b5762a9715765b81e60d3 to your computer and use it in GitHub Desktop.
PHP Cache Dynamic HTML Page

PHP CACHE DYNAMIC HTML PAGES

https://code-boxx.com/cache-dynamic-pages-php/

NOTES

  1. Create a database and import 1-content.sql.
  2. Change the database settings to your own in 3-dynamic.php.
  3. Access 4-cache.php in your web browser, this will generate a static cached-1.html on the first load.
  4. Following visits to 4-cache.php will load the static HTML instead of generating dynamically.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) CONTENT TABLE
CREATE TABLE `content` (
`content_id` bigint(20) NOT NULL,
`content_title` varchar(255) NOT NULL,
`content_body` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `content`
ADD PRIMARY KEY (`content_id`);
ALTER TABLE `content`
MODIFY `content_id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (B) DUMMY CONTENT
INSERT INTO `content` (`content_id`, `content_title`, `content_body`) VALUES
(1, 'Hello World!', '<p>This is a <strong>test page</strong>.</p>\r\n<p>Saved in a database.</p>\r\n<p>Dynamically generated page.</p>\r\n<p>Some random text.</p>');
<!DOCTYPE html>
<html>
<head>
<title><?=$_PAGE["content_title"]?></title>
<meta charset="utf-8">
</head>
<body>
<header>My Site</header>
<main>
<h1><?=$_PAGE["content_title"]?></h1>
<?=$_PAGE["content_body"]?>
</main>
<?php
// (A) DATABASE SETTINGS - CHANGE TO YOUR OWN
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (B) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) GET CONTENT FROM DATABASE
$id = 1; // CONTENT ID 1
$stmt = $pdo->prepare("SELECT * FROM `content` WHERE `content_id`=?");
$stmt->execute([$id]);
$_PAGE = $stmt->fetch();
// (D) GENERATE HTML PAGE
require "2a-page-top.php";
require "2b-page-bottom.php";
<?php
// (A) SERVE STATIC HTML IF EXIST
$id = 1; // CONTENT ID 1
$file = "cached-$id.html";
if (file_exists($file)) {
// echo "THIS IS A CACHED COPY!";
require $file;
}
// (B) GENERATE HTML FILE
else {
// (B1) START OUTPUT BUFFERING
ob_start();
// (B2) GENERATE HTML PAGE
require "3-dynamic.php";
// (B3) SAVE BUFFER INTO STATIC HTML
file_put_contents($file, ob_get_contents());
// (B4) OUTPUT CONTENTS
ob_end_flush();
}
<footer>Copyright, My Site.</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment