eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak


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