introduction to web programming non-parsed header scripts In this section, we will outline several ways to use CGI scripts to push data to a client such as when you want to animate text. Specifically, we will discuss nph (non-parsed header) scripts.

Surely, newer technologies, such as Java, or plug-ins, such as Shockwave, are better suited for complex animation.

However, the ability to use NPH scripts in creative ways is an important part of any CGI programmer's box of tools. NPH scripts add another dimension to multimedia and can make your site more diverse. They can also be used when the big guns of Java or Shockwave are not appropriate.

Nonparsed header scripts are used when we want to bypass the server.

Usually, when we use the line

     print "Content-type: text/plain\n\n";

we count on the fact that the server that executes the script will fill in the rest of the HTTP protocol lines, such as the "200 OK" status codes, the date and time, and other information defined in the protocol.

With NPH scripts, we must generate those HTTP protocol messages internally, bypassing server parsing. In other words, we output directly to the browser without Web server intervention.

Most of the information that you might pass to the browser is optional. However, you should at least return the MIME content type of the data, the HTTP protocol revision, the status of the program, and the server name and version.

By bypassing the server, you can communicate directly with the browser. As long as the browser listens, you can continue to feed it more information. In this way, you can use cell animation to create an animated series of text or images.

However, the use of NPH scripts requires that all scripts begin with the characters nph-. This is the convention used by servers to recognize an NPH script. If you rename the script, the server will not know to treat it as an NPH script and instead will run it as a normal script.

Because you are bypassing the server, you need to be careful that your NPH scripts do not run forever, because they will run to completion whether or not the browser has already moved to another page. If the script loops infinitely, it may never know to stop and may eat up your server resources.

Let's take a look at a simple nph script which animates a countdown:

print "$ENV{'SERVER_PROTOCOL'} 200 OK\n";
print "Server: $ENV{'SERVER_SOFTWARE'}\n";
print "Content-type: text/plain\n\n";
$| = 1;
for ($loop = 10; $loop >= 0; $loop--)
  print "$loop\n";
  sleep (1);
print "Blast Off!\n";
exit (0);

On the other hand, you may want to include HTML or images in your animation. In this case, you must specify a content type and a boundary:

$| = 1;
print "$ENV{'SERVER_PROTOCOL'} 200 OK\n";
print "Server: $ENV{'SERVER_SOFTWARE'}\n";
print "Content-type:

		# Begin sending words

print "--ARandomString\n";

		# Create a list of words that you
		# want to be animated.

@words = ("This", "is", "a", "test");

		# Set $loop equal to zero (since all
		# arrays start with 0 as their first
		# value).  Then begin incrementing
		# $loop by one ($loop++) until $loop is
		# equivalent to the number of words in
		# @words ($loop <= @words;)

for ($loop = 0; $loop <= @words; $loop++)

		# For every value of $loop, print out
		# the value in @words that corresponds
		# to the value of loop.  So when $loop
		# equals zero, print out the first
		# word in the @words array and when
		# $loop equals 1, print out the second
		# word...and so on.  Then pause a second
		# and print out the flag which will notify
		# the browser to replace the next word
		# with the current one.

    print "Content-type: text/plain\n\n";
    print "$words[$loop]\n";
    sleep (1);
    print "\n--ARandomString\n";

		# Quit-a-mundo

    exit (0);

Previous | Next | Table of Contents