The Universal Web Form Processor12/29/2000
One recent evening the Phanatic and a few friends were splitting an extra large pizza and a few six packs of Jolt. The Phanatic was pontificating as usual about good program design and problem generalization. One of the PHP apostles, probably egged on by a Jolt buzz, challenged the Phanatic to "practice what you preach" and design a program that solved a class of problems rather a single task. Another of the assembled brethren suggested the Phanatic use the NuSphere suite he had been extolling recently. The gauntlet was thrown down.
The Phanatic like any good programmer can't refuse a good challenge. The only thing more motivating than a challenge was the statement, "it's impossible." After a little discussion, the huddled mass yearning to program proclaimed the task would be a form-process script that "does everything."
Before getting into the generic form script, let's take a look at the Phanatic's new favorite product.
The product making the Phanatic so excited is NuSphere MySQL. It's an integrated multi-platform distribution of Apache, Perl, PHP, and MySQL. Each element of the distribution package is the pick of the litter in their respective open source application category.
NuSphere distributions are available for Linux, Unix, and Windows platforms. If you have a lot of patience and a high-speed connection, the package can be downloaded for free. The CD ROM boxed edition is $79 and includes a hard copy of the massive MySQL Reference and the O'Reilly pocket guides for Apache, Perl 5, and PHP 4.
The Windows downloadable version of NuSphere's MySQL weighs in at about 24 megs without source code and 50 megs with source code for all products. Unzipping the package generates over 4,500 files and consumes 65 megs of disk space! Although this sounds like a lot of bits and bytes, (especially to a guy who's first hard drive was 32 megs), let's put it into perspective. The NuSphere distribution includes the four major web-related offerings in the open source world. Collectively they consume less disk space than that big software company's word processor.
For good measure, the installation includes the complete online documentation for all four products and, additionally, a graphical administration tool for managing MySQL. Here's the best part: within two minutes of unpacking the zipped download, you can be testing the MySQL, Perl, and PHP on an Apache server running on your PC. If you have ever struggled with the installation and configuration of just one of these products, you will think the NuSphere installation is magic.
The first step in the NuSphere suite is unzipping the downloaded zip file. I would suggest you create a directory
C:\NuSphere and extract the files into that directory. The extraction process will create two directories under
n.nn.n (something like
1.13.2) is the MySQL version information. Next, run the file
setup.exe from the directory:
A browser window will open with the salutation, "Welcome to the NuSphere MySQL installation CD." Click on the Install button. The next window offers the option for a Quick or Custom installation.
Unless you are highly experienced, take the Quick installation option. The default path for the Quick install is
C:\nusphere. Click on the Quick install button accepting the default path. MySQL, Apache, Sample Website, PHP, phpMyAdmin, and Perl are installed in turn. Next, follow the "Click here to continue" link. The next screen has a link to register and a link to do a quick start. After registering, click on "Quick Start."
A new window will open with the default home page. Check out the various links to test your installation. The startup page also has links to full documentation for MySQL, Perl, PHP, Apache, and the phpMyAdmin applications which reside on your PC. This is truly an impressive package.
What you do next is a function of your background and experience with Apache and the other included packages. Let's discuss paths for a moment. At the server level it's important to recognize the difference between action and virtual paths. When you point your browser to a URL like,
http://www.xyz.com/index.html, there is a lot going on under the surface. Where does
index.html actually live? The server configuration file contains an entry associating an actual starting path for the domain xyz.com portion of the URL. It might be something as simple as
although the path is frequently more complicated.
To answer our own question,
index.html lives at
home/xyz/htdocs/index.html. The virtual web root is therefore
/home/xyz/htdocs. If we were to create a directory named
htdocs and also create a file named
forms.html in that directory, the URL would be
Since Apache is now running on our PC, as opposed to running on an ISP's machine, there is no domain name. When we want to access the Apache server running on our local machine, we use
localhost in a URL where we would normally use a domain name.
If we enter a URL like
what is the file we are actually viewing? During the NuSphere installation we specified an application root directory of
C:\nusphere. The installation creates an
apache subdirectory and a directory of
apache. The virtual root of this system, without changing the Apache configuration, is therefore
The Apache configuration file also indicates a default start page of
If you have a single web site, you can use
C:\nusphere\apache\htdocs\ as your web site's starting directory. Additionally, build a directory image under directory "htdocs" that conforms to your actual directory tree structure. If you have multiple web sites you will probably want to create a directory under htdocs for each of your domains. You could also do some Apache configuration to deal with these issues, but I would not recommend you go that route unless you have experience with Apache configuration.
A few last points. The domain
localhost is a virtual domain, there is no directory named
localhost. If you are going to use the
htdocs directory as your testbed, rename
index.html, it's the NuSphere suite starting page.
Apache and MySQL must be started before you can use them. When you start them depends upon you work habits. If you do web site development almost every time you log on, you may want to have the severs started when you boot up. If so, create an item in your Start Up group. If not, create a shortcut on your desktop. The following will start all the required applications in one shot.
After starting NuSphere we can use the various applications for developing the form-to-mail PHP script. Create the PHP files as you normally would. Let's assume you create a file
test-form.php. Instead of uploading them to your friendly ISP, create a directory under
C:\nusphere\apache\htdocs called php or whatever directory name you prefer. You can place your files in the
htdocs directory, but it will quickly start to get disorganized.
Let's assume you put have tucked away test-form.php as
C:\nusphere\apache\htdocs\php\test-form.php. Place the following URL in your favorite Web browser:
It's great not to have to keep uploading every minor revision to your ISP for testing.
Forms R Us
The Phanatic installed the NuSphere package on a AMD Athlon 700 MHz system with 128 megs of RAM. The OS is Windows 98 SE.
Well we've had our little software diversion, the pizza is finished, and there is no more Jolt, so it looks like there are no excuses left for the Phanatic not getting back to why we're here, PHP. Let's get to work on the e-mail form handler.
A form processing do-it-all script st a big task. Let's start by conceptualizing the characteristics of this beast. The script should, based upon the form designer's choice,
- display a nicely formatted output of the form's variables;
- email a nicely formatted output of the form's variables to a designated recipient;
- email a nicely formatted receipt to the form's originator;
- redirect to a designated document;
- display a "thank you" message;
- validate designated fields for non-null, email address, zip code, and numeric data;
- allow user selection of cosmetic properties;
- allow users maximum latitude in the selection of form names; and
- include selected environment variables in output display.
Any program worth its creation time must overcome a series of problems, hopefully using elegant solutions. Repeat slowly after me, "elegance is simplicity!" Recall the three desirable program characteristics from a previous tome: effectiveness, maintainability, and efficiency. Any non-trivial problem will have multiple solutions. Program elegance is therefore finding the best solution, not simply one that works. Let's look at obstacles this script will have to overcome.
- Detect if the form was submitted using the GET or POST method.
- Delineate between HIDDEN control variables and unknown user variables.
- Allow only email requests from designated domains, for security reasons.
- Deal with form elements having multiple values.
- Validate selected fields such as e-mail addresses.
The Phanatic will present some code snippets to solve each of these problems. Since there is a lot of work, and space is limited, the next episode concludes with a complete script ready for some prime time form evaluation.
Pages: 1, 2