Forum Moderators: coopster

Message Too Old, No Replies

Create html copy of current dynamic php page ?

         

Sgt_Kickaxe

8:30 am on Feb 28, 2011 (gmt 0)



Basically I'd like my dynamic php built pages to create a static .html copy of themselves as they are loaded, if a copy doesn't already exist.

I know how to check for a static copy before building the dynamic page.

I'm having trouble getting the current page to save properly however, any ideas?

Specifics: I'm using "file_get_contents($_SERVER['PHP_SELF']);" at the very bottom of the dynamic page code to try and get a copy of the current page into string form, is this just creating a never ending loop?

I'm also trying to save the .html file with the same url that the current page has which sounds like it shouldn't work but you have to remember that the current url doesn't exist. ie: example.com/big-red-widgets doesn't exist because it is actually example.com/dynamic-script.php with .htaccess help.

best effort...
<?php
$homepage = $_SERVER['PHP_SELF']
$homepageContent = file_get_contents($_SERVER['PHP_SELF']);
$fp=fopen($homepage,'w+');
fputs($fp,$homepageContent);
fclose($fp);
?>

coopster

12:03 am on Mar 2, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A lot of times if you are attempting to get a page from your own server via http protocol the request won't be followed. When you try to retrieve a URI from the same domain and it fails, the function may be returning boolean false rather than the string contents of the URI requested. This happens because of dns or routing somewhere down the chain (firewall) and the quick, easy fix is to add a line to the hosts file.

However, a better approach here it not to use an external request at all. Rather, turn on output buffering and at the very end of the script push the buffered output to a variable that you can then write to your file.

Check out ob_start [php.net] and friends.