| Lesson 2 | Program flow |
| Objective | Explore why Program Flow is of Special Concern in CGI |
Program flow is a special concern in CGI because each request triggers a completely new process that starts from scratch, executes once, and terminates immediately after sending its output. Unlike traditional applications that maintain persistent state and continuous execution, CGI scripts cannot rely on variables or open connections persisting between requests, making careful management of initialization, processing sequence, and cleanup essential. This stateless nature demands that all necessary data, such as user input from forms or session information, be explicitly passed or retrieved (often via environment variables, query strings, or cookies) within the tight lifespan of a single invocation. Errors in program flow, such as skipping validation steps or mishandling output formatting, can easily result in incomplete responses, server errors, or security vulnerabilities since there is no ongoing runtime to recover or retry. Consequently, developers must design exceptionally linear, robust, and self-contained logic to ensure reliable behavior under the unique constraints of the Common Gateway Interface model.
In traditional programming, "program flow" refers to the continuous sequence of execution from start to finish. A conventional application runs continuously, maintaining its state in memory, responding to user actions, and controlling the sequence of screens and interactions the user experiences.
For example, a traditional desktop application might:
Throughout this entire sequence, the program remains running in memory, maintaining context and controlling which screen appears next based on the user's actions and the data's state.
CGI presents a fundamental challenge: CGI programs do not flow. Each time a user requests a page or submits a form, the web server:
Once your CGI program displays a screen to the user, it's finished - it exits completely. There's no program sitting in memory waiting for the user's next action. There's no continuous execution flow. Each interaction starts from scratch with a brand new process.
This execute-and-exit model is efficient for the web server, but it creates significant challenges when you want to build applications with multi-step interactions.
Consider a simple Guestbook application. You want to provide users with a smooth, intuitive experience:
These are all problems of program flow. In a traditional application, you'd simply control which screen to display next based on the current state. But in CGI, there is no "next" - your program exits after generating each screen.
When a user enters data into your form, the data gets passed to your CGI program. You could simply save it to a file or send it in an email message - but does that really give the user the necessary feedback to know what's happening?
What if the user made a mistake? What if they want to review their entry before submitting it? What if they need to make a change?
When you give users proper feedback and an opportunity to review their data and confirm it's correct, you create a sense of quality that tells users you care about them and the accuracy of their information. This level of interaction requires solving the program flow problem in CGI.
The most common reason program flow isn't properly handled in CGI applications is that the tools make it difficult. Traditional CGI programming offers no built-in mechanism to:
Because HTTP is stateless and CGI programs exit after each request, you must explicitly build these capabilities into your application.
There are two fundamental techniques for overcoming the lack of flow in CGI applications:
These techniques work together to give you control over program flow despite CGI's execute-and-exit nature. By tracking state and generating appropriate HTML for each state, you can create the illusion of a continuously running application.
Let's begin with the state-machine model in the next lesson.