Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Dynamic Generation Of Static Webpages
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:

<?php

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

  define('
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

  $staticdir="C:/windows/desktop/samplewebsite/generatedpages/online";
                                            //* 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;
     $handle=opendir($dirname);
     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;
            }
            searchdir($dirname.'
/'.$file);
         } else {
          if (substr($file, -5)==".php4") {
             $newname=substr($file, 0, strlen($file)-5).'
.html';
             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;
          }
        }
      }
     closedir($handle);
   };

  searchdir($dyndir);
?>
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
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.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
Problem posting formsOlanrewaju, Michael Oluseyi02/28/05 14:25
about phpgaya02/16/05 03:08
The best php site copierLeonardo Forero02/28/03 22:34
RE: dynamic/static web sites?china_boy12/30/02 04:18
Need some advice please Dave McKergan05/09/02 09:04
RE: Use pavuk wget plus moreJames Wilford04/09/02 21:30
RE: dynamic/static web sites?Ellen Richardson02/27/02 12:06
what if you don't have linuxEdith12/06/01 09:13
dynamic/static web sites?John11/30/01 17:28
RE: wget to the rescueWhat if the site has passwords ?11/26/01 16:01
Url argument and javascript is solutionrachmadji wibowo Ph.D11/02/01 04:40
RE: What about URL arguments??-a solutionVladimir10/04/01 11:08
static and dynamicdavid10/03/01 16:14
lettersuhas09/15/01 10:48
Use pavuk wget plus moreA K08/22/01 06:39
RE: FrontpageNeil08/15/01 23:29
RE: What about URL arguments??Stephen Webb08/08/01 23:26
a different approach: Speeding up page loadsAlfredo Rahn07/31/01 05:55
IP Address configurationSaravanan07/31/01 02:37
RE: If you have mod_php you can use long URL'sRussel07/29/01 00:03
RE: If you have mod_php you can use long URL'sRussel07/28/01 23:56
RE: why re-invent the wheel?Raven07/11/01 09:52
RE: No need for Lynx, use fopen(http://)Bernhard Dreyer06/12/01 10:15
just run php directly manRadek Burkat06/11/01 12:10
If you have mod_php you can use long URL'sBill Peck06/07/01 16:39
RE: What about URL arguments??Rick05/25/01 07:52
static or dynamic?Ryan Thomas05/14/01 07:03
RE: wget to the rescuecertron05/13/01 09:10
RE: why re-invent the wheel?lanson05/07/01 23:57
Keep semi-static o r semi-dynamicTjin05/04/01 14:43
if your html on a web cd use javascript!!!Daniel05/02/01 15:24
why re-invent the wheel?foo05/02/01 07:57
Does anybody have a simple example?Ali Driver05/01/01 00:44
RE: wget to the rescueStefan Waidele04/30/01 21:29
RE: What about URL arguments??Jonathan Melhuish04/30/01 15:32
FrontpageTeo Danardi04/27/01 16:09
RE: What about URL arguments??Loris Tissino04/27/01 00:52
RE: keep it simpleLoris Tissino04/27/01 00:48
RE: wget to the rescueLoris Tissino04/27/01 00:46
RE: just use the php binary!Loris Tissino04/27/01 00:45
RE: What about URL arguments??Thomas Donadt04/26/01 16:18
RE: What about URL arguments??Andreas Bernhardsen04/25/01 10:36
RE: wget to the rescueAli Driver04/25/01 07:26
RE: just use the php binary!DrTebi04/25/01 05:12
RE: wget to the rescueeric04/24/01 14:17
RE: wget to the rescuedave04/24/01 00:49
RE: What about URL arguments??Thomas Donadt04/23/01 16:04
just use the php binary!Larry04/23/01 14:38
Good Idea...Webserver,.XML Dynamics,c++John04/22/01 07:35
No need for Lynx, use fopen(http://)John Lim04/22/01 01:05
I don't exactly get itMiguel Cruz04/21/01 23:35
What about URL arguments??Andy Dickinson04/21/01 19:29
keep it simpleTom04/21/01 12:57
Lynx vs. other toolsStralle04/20/01 20:26
wget to the rescueScott Marlowe04/20/01 17:18
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.