PHPBuilder - Building Static Sites Page 2

RSS Twitter
Articles Html

Building Static Sites - Page 2

by: Loris Tissino
April 20, 2001

The Idea

The idea is simple. You develop your website at home, using all PHP features you like, such as inclusion of text files, extensive use of pre-defined and user-declared functions, classes, templates, and so on.
Once you are satisfied with your work, you could use your preferred browser to view all your pages, one-by-one, and save them in a directory of your hard disk, changing file extensions from .php4 to .html. But this approach would be quite time-consuming, expecially if you have several pages and need to re-generate them quite often.
Using lynx is a good alternative, since it can fetch the pages from the webserver and return them into the standard output. You could then write a batch procedure (a shell script) such as:
lynx -source http://localhost/dynpages/index.php4 > C:\Documents\StaticPages\mywebsite\index.html 
lynx -source http://localhost/dynpages/page2.php4 > C:\Documents\StaticPages\mywebsite\page2.html 
lynx -source http://localhost/dynpages/page3.php4 > C:\Documents\StaticPages\mywebsite\page3.html 
lynx -source http://localhost/dynpages/page4.php4 > C:\Documents\StaticPages\mywebsite\page4.html
The example here is for Windows, under other platforms you should obviously change the path after the > operator.
This way, when you want to regenerate all your pages, you just need to launch your batch file.
If you have a number of files, and/or their names change often, you could think about using PHP to generate the lines of your batch file:


('SLASH''\');      //* this is for use under Win9x;
                              //* under linux, use '

CRLF', chr(13).chr(10));

  $httpbase="http://localhost/samplesite";  //* local URL for your pages

  $dyndir="E:/xitami/webpages/samplesite";  //* (do not use backslashes)
                                            //* where your webserver keeps your pages

                                            //* where you want your pages to be put

  $parameters="?outputas=online";           //* parameters you might need

  function changeslashes($path) {
     return str_replace("/", SLASH, $path);  //* useful under Windows

  function subdir($path) {

     global $dyndir;

     if ($dyndir!=$path) {
          return substr($path, strlen($dyndir)-strlen($path));
     else {
          return '';


  function destination($path)  {

     global $staticdir;

     $dest=$staticdir . subdir($path);

     $dest=changeslashes($dest);  //* useful under Win
     return $dest;     

  function searchdir($dirname) {

     global $httpbase, $parameters;
     while ($file=readdir($handle)) {
       if ($file !='
.' && $file !='..') {
         if (is_dir($dirname.'
/'.$file)) {

            if (!is_dir(destination($dirname).'
/'.$file)) {
                echo "mkdir " . destination($dirname). SLASH . "$file" . CRLF;
         } else {
          if (substr($file, -5)==".php4") {
             $newname=substr($file, 0, strlen($file)-5).'
             echo "lynx -source $httpbase" . subdir($dirname) . 
                "/$file$parameters > " . destination($dirname) . SLASH . "$newname" . CRLF;
          } else {
             echo "copy " . changeslashes($dirname) . 
                SLASH. "$file " . destination($dirname) . SLASH. "$file" . CRLF;

The script is recursive, and will generate a batch file to generate all your PHP pages (making them normal HTML files) and to just copy all other files.
You might capture the output of this PHP script into a batch file and run it at once. For instance:
PHP -q generatepages.php4 > generatepages.bat
This can be a batch procedure on its own, and so you need only a command to generate all the pages of your web site.
I wrote and tested this script under Windows. It should also work under Linux, provided that you change something here and there (you wouldn't need to change slashes to back slashes, you would use cp instead of copy, etc.).
As you may notice, you can encode in the URL address (the query string) one or more parameters, as usual. I often take advantage of this, using parameters to control which stylesheet to use, or which content to display on my pages.
To make this clear, I'll show you the values I give to the outputas parameter:
  • online
    to use when you generate static pages to be published at an internet site (links have to get .html extension, links to directories have no file name added)
  • offline
    to use when you generate static pages to be viewed off line, with no webserver involved (links have to get .html extension, links to directories have to be added index.html)
  • print
    to use when you want to print the pages you are generating (I choose a different stylesheet, and write remote URLS in brackets after linked text)
Since the value of parameters is available for all the functions you call in your webpage, you can make good use of this.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Loris Tissino



(Maximum characters: 1200). You have characters left.