Home arrow PHP arrow Page 2 - Static HTML Generation With PHP
PHP

Static HTML Generation With PHP


In this article Eric, expresses how to may reduce the workload of your server by using PHP to CRON your PHP based web site. He will demostrate how this is done by refering to his own web site.

Author Info:
By: Eric 'phpfreak' Rosebrock
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
April 10, 2003
TABLE OF CONTENTS:
  1. · Static HTML Generation With PHP
  2. · Page 2

print this article
SEARCH DEVARTICLES

Static HTML Generation With PHP - Page 2
(Page 2 of 2 )

Earlier I mentioned that I would explain why I use the $url variable instead of typing it directly into the $sourcepage variable. We can prepare a script on a development server and change one varialbe, $url, to reflect the live server when we upload the static generation scripts.

We'll also want to consider what pages to update at what time intervals. I'll usually run a 30 minute, 1 hour and 12 hour cron scripts and decide which sections of my website I want to generate at what intervals.

Let's take a look at the 1 hour cron php script.

<?php
//1 hour cron script
$url = "
http://www.mysite.com
";
/* Let's generate the Tutorial Index
for phpfreaks.com by creating it's own
static generation function */
function tutorial_index($url){
$sourcepage = "$url/static_generator/tutorial_index.php";
$tempfilename = "/tmp/temp_tutorials.html";    
$targetfilename = "/www/htdocs/mysite/static_html/tutorial_index.html";
if (!$dynamic_source){ 
echo "<strong>Unable to load $sourcepage 
- Static page! Update Failed!</strong>";    
exit();
}
$htmldata = fread($dynamic_source, 1024*1024); 
fclose($dynamic_source);
$tempfile = fopen($tempfilename, 'w');
if (!$tempfile) { 
echo"<strong>Unable  to open temporary file $tempfilename for writing!
Static page update aborted!</strong>";    
exit();
}
fwrite($tempfile, $htmldata);
fclose($tempfile);
copy($tempfilename, $targetfilename); 
unlink($tempfilename); 
echo "<strong>Tutorial Index Updated!</strong>";
}
/* Let's generate the User Submitted
Tutorial Index for phpfreaks.com by creating
it's own function */
function user_tutorials($url){  
$sourcepage = "$url/static_generator/user_tutorial_index.php"; 
$tempfilename = "/tmp/temp__user_tutorials.html";    
$targetfilename = "/www/htdocs/mysite/static_html/user_tutorial_index.html";
if (!$dynamic_source){
echo "<strong>Unable to load $sourcepage 
- Static page! Update Failed!</strong>";    
exit();
}
$htmldata = fread($dynamic_source, 1024*1024); 
fclose($dynamic_source);
$tempfile = fopen($tempfilename, 'w');
if (!$tempfile) { 
echo"<strong>Unable  to open temporary file $tempfilename for writing!
Static page update aborted!</strong>";    
exit();
}
fwrite($tempfile, $htmldata);
fclose($tempfile);
copy($tempfilename, $targetfilename); 
unlink($tempfilename); 
echo "<strong>User Submitted Tutorial Index Updated!</strong>";
}
tutorial_index($url);
user_tutorials($url);
?>

Here's a brief explanation of what I have done above. I have taken the original script and configured it as a function based PHP script. Here I am able to run two different static html generations inside of a single PHP script, but I leave myself options.

The first option is that I can change the $url easily by editing one line at the top of the page. The second option is that I can go back and add or remove which pages I want to generate very easily by either removing the lines at the bottom which call the functions that I created above.

If I am having problems with a particular static generation page, I don't have to delete the entire code, I can just comment out the line at the bottom of the script that calls the specific function. Here's an example:

Let's say that for some reason my User Submitted Tutorials (Second function) is giving me errors. I can simply do this to the user_tutorials($url); function call at the bottom:

//user_tutorials($url);

And that function will be temporarily disabled.

As you can probably figure out by now, you can really do yourself alot of good by creating custom functions and using them at your leasure. There's no limit to how many functions you have in a PHP script as long as the functions don't contain duplicate names with custom functions or predefined functions that PHP has built in such as header() or copy() and the hundreds of other ones.

I may as well mention the last step in this static generation technique is that you'll need to include the HTML files in place of the original PHP scripts that perform the queries. A good idea is to always use relative paths to the site root for your PHP scripts that generate the html. You'll always want to preface the images and links with / (example: /path/to/image/logo.gif) because this method allows you to move files into any hierarchy of your directory structure without breaking links.

Let's discuss some other options.

A couple of things you might want to consider. You may want to have access to your CRON scripts via your web browser. Let's say that I added a new tutorial today and I want it to be visible NOW. I'll want to run that 12 Hour CRON script that I have my tutorials setup on so I can geneate the index file and allow this tutorial to be accessible without losing time where users can get that valuable information.

Of course you'll probably also want to password protect the directory where that script resides in so that someone doesn't try to crash your web server by maliciously running your script over and over again and rapid intervals. Do yourself a favor and take security into consideration.

Take a close look at your website and decide what you want to be updated EVERY time a reader visits your site and consider what you can live without being updated every time the page loads. If you run a heavy traffic site, you'll want to take this seriously. Your server can litterally start to crawl as those thousands of mysql queries are done each minute.

If you would like to learn how to run this script on an actual CRON on linux/unix, please read my tutorial on. Automate Your PHP Site Using CRON.

Have fun with this! It may seem like a little bit of work, but it's worth it. I encourage you to post comments of success or failure below. If you have any alternatives, please post them below instead of contacting me directly via Email.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials