Jump to content

Creating a snapshot of the database


bachx

Recommended Posts

I want to create a page that updates every 15 minutes only, so that whenever you view that page, you'll view a mirror/image of the last update (not the current data). This is done to reduce the load on the site, as that page contains many queries/etc, so running those queries every time someone view this page will slow the site down. Any ideas how can this be done? Need help ASAP.

Link to comment
https://forums.phpfreaks.com/topic/66401-creating-a-snapshot-of-the-database/
Share on other sites

Well, I wasn't really able to create such a script. I've seen such pages (For example, those that updates the Users Online page every 5 minutes only), I know there is a cron script that runs every 5 minutes, but I couldn't manage to overwrite the file using 'fwrite' since it's not simply a simple string, but a full page with HTML/Javascripts/etc.

 

Any ideas? Would be greatly appreciated.

No need to modify the file, recreate it. Make a function with all your html and javascript and whatever and run it with cron:

 

function createFile($text, $file){
       $text = echo "<html><head><title>My Site</title></head><body>$text</body></hml>";
       $handle = fopen($file, 'w+');
       fwrite($handle, $text);
       fclose($handle);
       return true;
}

 

It is a simple example but surely it can be done even in more complex scenarios.

At the top of your page script put

 

<?php
ob_start();
?>

 

At the end, after the final /html tag put

<?php
$pagecontent = ob_get_contents();                    // get the contents of the page
file_put_contents('mypage.html', $pagecontent);      // save to file
ob_end_flush();                                      // output the page
?>

 

When you run the script the rendered contents are saved in mypage.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.