Jump to content

[SOLVED] Convert Dynamic PHP page to Static HTML


isedeasy

Recommended Posts

Hi Everyone,

 

I have a page that pulls the content from a MySQL db, it works fine how it is but I want my site to be more efficient.

 

When I add new content (via a form in the admin area) I want the site to generate a static HTML copy of the PHP page. This way the page only needs to call the db when I update the site rather than everytime somebody loads the page.

 

I found a few scripts that claim to do this but none of them seem to parse the php before creating the HTML page.

 

Has anyone any pointers on how to go about this?

 

Cheers

You would need to include the file using the URL to the .php page, then use ob_get_contents() to capture the resulting output - http://www.php.net/manual/en/function.ob-get-contents.php Then simply write the captured content to the file of your choice.

I actually just did this.

Put this on top for where you want to start capturing output:

 

ob_start();

 

put this on the bottom of the php

 

$HtmlCode= ob_get_contents();

ob_end_flush();

 

$fh=fopen('NewPage.html','w');

fwrite($fh,$HtmlCode);

 

fclose($fh);

 

 

Cheers for the code twittoris, I want to make a function so that I can use it for multiple pages. I came up with the following which works but when I call it, it inserts the the code into the page I call it from as-well. Is there a way to stop this or a better way to achieve what I am after?

 

function wwwcopy($dynamic,$static) {

    ob_start();

    require $dynamic;

    $temp= ob_get_contents();
    
    ob_end_flush();

    $fh = fopen($static,"w");
    fwrite($fh,$temp);
    fclose($fh);
    
    }

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.