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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.