table of Contents
What is the CGI script?
How does the CGI script work? A simple example I can use the CGI script?
Does your server configuration allow CGI scripts? Can you program? What programming language do you use? Is your server settings correct? What if your server is not a UNIX system? Anatomy CGI script
Output header output data unit The script is delivered to the script to create a special script output
Scripts that load another text response without response processing form
Form format and form script GET and POST URL encoding problem CGI variable decoding program
Uncgi cgi-lib.pl decoding files uploaded by the input yourself to do non-anatomy head script summary
Learn CGI scripts (script)
CGI means a browser-based input, a program method that is running on a web server. CGI script allows your browser to interact with users, in order to find a noun in the database, provide you write comments Or choose a few entries from a form and get a clear answer. If you have encountered a search or search on the Web, you just use the CGI script. You may not realize that because of the big Some work is running on the server, just what you see.
As a web designer, you create a client's CGI script, the server's program is used to handle user input, and the result is returned to the user.
Here you will learn everything about the CGI script:
What is the CGI script? What does it work? What is the CGI script output? How to create a CGI script with parameters or non-parameters How to create a CGI script that returns a specified response to create a CGI script for entering a form about the CGI variable you can use in the script.
This chapter assumes that under UNIX systems.
What is the CGI script?
The CGI script simply is a program running on the web server, with a browser input trigger. This script usually likes the bridge of the database such as the database in the server and system.
Isn't the CGI script not a real script? According to your server support, they may be a compiled program or batch command file or other executable things. For the sake of simplicity, we collect them as scripts Scripts.
The CGI script is any program running on the web server. CGI means Common Gateway Interface.
The CGI script is used in the following two methods: as an action of a form or as a direct LINK in a page.
How does the CGI script work?
The CGI script has server calls, Browser-based data input. Figure 1 shows a process between the browser, server, and scripts.
Figure 1. From the browser to the server to the script to the program to remember and then come back!
This has a short schematic explanation:
A URL points to a CGI script. The URL of a CGI script can appear anywhere as a normal URL. The server receives the request, and performs scripts according to the script file pointed to by that URL (note files and extensions). Scripts Perform an operation based on input data, including the query database, calculate value, or other programs in the system. Script produces a certain web The server can understand the output. The server receives the output from the script and transmits it back to the browser, allowing the user to understand the results.
A simple example
Here, explain all the details related to the occurrence of the occurrence step by step.
figure 2:
Display Date is a connection pointing to the CGI script. Its HTML is like this:
Display the date Description is a CGI script because there is a CGI-BIN path path. In many servers CGI- BIN is the directory that can only place the CGI script.
When you choose this connection, your browser will request a request to www.popchina.com servers. The server receives this request to calculate the script file name at the URL and then execute this script.
This getDate script is implemented in the UNIX system:
#! / bin / sh
Echo Content-Type: Text / Plain
echo
/ bin / date The first line is a special command, telling the Unix system This is a shell script; the real situation is the next line starting from this line, this script is two things: it outputs line content-type: Text / Plain, Then start an empty line; second, it calls the UNIX system time Date program, which outputs the date and time. The output of the script should be like this:
Content-Type: Text / Plain
Tue Oct 25 16:15:57 EDT 1994 This content-type is Dongdong? It is a special code, the web server is used to tell the browser to output this text. This is the same as the content-type meaning in HTML.
Such a browser output is as shown in Figure 3.
This is the most basic, the actual situation is much more complicated, in short, how can you understand how the browser, server, and scripts work.
Can I use a CGI script?
Before you use the CGI script, there are two things you might want to resolve: The CGI script is a high-level web feature and requires you as good as the web server manager.
Are you definite? It's not until you do. Ok! let's continue.
Does your server configuration allow CGI scripts?
In order to write and run the CGI script, you need a web server. You don't like the usual HTML file, you can't write or trial your CGI script on your local system; you have to do this through the web server.
But even if you have a web server, this server must be specifically configured to run the CGI script. That means that all your scripts must be placed in a directory called CGI-bin.
Before writing the CGI script, ask if your server manager allows you to install and run the CGI script, and if you can, where do they have? Also, you have to have a real web server, if you are an FTP or GoPher server, then you can't use CGI.
If you are running on your own server, you must specifically create a directory called cgi-bin and configure your server to approve this directory as a script directory. You must also remember the following related CGI script features:
Each script is a program that runs on the browser to request, when executed, using CPU time and memory. What if there is a thousand these scripts running at the same time? Your system will not bear the load until it crashes. If you don't carefully write your CGI script, you will be possible to let others enter your system through your CGI script parameters.
Will you program?
Beginners pay attention! Generally, you must have some basic programming concepts and methods. You have to have similar systems work. If you don't have these backgrounds, you have to learn, ok, don't say much.
What programming language do you have to use?
You can write CGI scripts in any language you are familiar with, as long as your script follows the rules of the next section, as long as that language can run on your web server system.
In this learning manual, only two languages are written in both languages: UNIX Shell and Perl language. This shell is suitable for running on any similar UNIX system and is easy to learn, but it is difficult to handle complexity. Perl, To use this language, it is free, this language is stable and powerful, similar to C, but it is also difficult to learn.
Is your server settings correct?
In order to run any CGI script, your server must be set to be able to run them without a simple or complex, your server must be placed in a specific directory, must have a file extension that relies on your server set.
If you are a rental server, you have to allow the CGI script to be run.
If you have your own server, check how your server manual processes the CGI script.
If you are not unix?
I have to find another learning manual.
Anatomy a CGI script
If you write it for a long time, overcome a lot of warnings and configurations, congratulations, you have some CGI scripts, and you can use it on your web page. In this chapter, how is the learning script is executed, what is your server? Respond with their dialogue. Output head
Although your CGI script allows you to do anything, the output of the script must still have a predetermined form.
This "script output" means that your script sends back the server's data. In the UNIX system, the output is the output of the standard output, the server detects it. In other systems and servers, your script output may be different.
This head is actually part of the text, which is the information protocol between the server and the browser. You can't see it.
There are three types of heads: Content-Type, Location, and Status. Content-Type is most common.
About Content-Type explanation You can see the description of HTML, a specific code like you can issue:
Content-Type: TEXT / HTML In this example, the type of output data is text / html; in other words, he is an HTML file.
Table 1. General Format and Content-Types.
FormatContent-typeHtmlText / HTMLTextText / PlapingIfimage / Gifjpegimage / JPEGPOSTScriptApplication / PostScriptmpegvideo / MPEG
Note that Content-Type must be followed by one blank line. If you don't have a space line, the server will not be able to figure out where this head is over.
Output Data
The data you export should comply with the content-type you specified; if Content-Type is Text / HTML, the output placed should be in HTML. If Content-Type is image / gif, the output should be in a binary GIF file.
Exercise 1: Try a small.
T This is a simple script of a simple output date. This CGI script also checks to see if I have already logged to my web server and report what I found (as in Figure 4).
This is a very simple example, he can prepare this:
is laura logged in? This is a script without input, it only runs and returns data.
According to the foregoing elaboration, this script content is this: #! / Bin / sh
Echo Content-Type: Text / HTML
echo "
"echo "
Echo " head>
Ison = 'WHO | GREP Lemay' test results and the script that returns the corresponding prompt is this:
IF [! -z "$ ison";.
echo "
laura is logged in." p>
Else
echo "
laura isn't logged in." p>
Fi finally closed HTML:
echo " body> html>" Now you run him from the command line, test it, you will get a result that I didn't log in to your system, of course, it is impossible, his output is like this:
Content-Type: Text / HTML
HEAD>
laura is not logged in.
Body> html> This is an HTML text that is output so that your browser will display him normally because he is an HTML file.
Now, in the cgi-bin directory of your server, he doesn't reach the CGI-BIN directory, you must ask your server manager, you can't make it yourself to build a cgi-bin directory yourself. use.
This example is the following script as follows:
#! / bin / sh
Echo "Content-Type: Text / HTML"
echo
echo "
"echo "
echo " head>
ISON = 'WHO | GREP LEMAY'
IF [! -z "$ ison";.
echo "
laura is logged in"
Else
echo "
laura isn't logged in"
Fi
echo " body> html>"
Script with parameters
In order to pass a parameter to the script, you can use (?) Between the URL between the scripts and parameters, indicate each single parameter with the plus sign ( ), such as:
Run My Script When the server receives this request, it passes Arg1, Arg2, and Arg3 parameters to the script. You can then be on the script Use these parameters in.
This method sometimes called queries because it is used in the search function.
Exercise 2: Check if someone landed.
Since you know how to use parameters, let us continue the example of Pinglaura, by modifying this example we get this script pinggeneric.
We take a different topic:
#! / bin / sh
Echo "Content-Type: Text / HTML"
echo
echo "
"echo "
Echo " head>
ISON = 'WHO | GREP "$ {1}"'
All remaining modifications are as follows:
IF [! -z "$ ison";.
Echo "
$ 1 is logged in"
Else
Echo "
$ 1 isn't logged in"
Fi
Echo " body> html>" Ok, let's modify the connection in the HTML page! It turned out to be like this:
is laura logged in? This is the case after the universal query function, such as the query name called John to log in: is john logged in?
Try it on your server to see if there is a result.
Pass other information to the script
There is a second way to pass the information to the CGI script. It is called path information Path Information as a parameter that is not changed in script calls, like a temporary file name or calling script. As you can see, The parameters behind the question mark are changed by the input of the user form. Path Information Path Info is used as other information to pass to the script, in fact, you can use it as anything.
Path Information Path Information is a method of frequently transmitting information as usual. Path Path Information typically refers to such files such as profiles, temporary files, or scripts, such as configuration files, temporary files, or scripts.
Look at the following path information Path Information example,:
http: // myhost / cgi-bin / myscript / remaining_path_info? arg1 arg2 When the script is run, the information in the path will be placed on the environmental parameter PATH_INFO. You can use this information in your script content.
For example, let us assume that you have multiple connections to the same script on multiple pages. You can use this path information to display the connected HTML file name. This way, after you complete your script, when you send When I go back to an HTML file, you can include a connection in this file, send back the user from the beginning.
You will learn more path information in the next chapter: useful forms and scripts.
Create a special script output
Now you have learned the data that is generally HTML data to send to the browser, such as output data. But if you don't want to send the script results as a data stream back to the browser, try to send a page that exists back, how Do it? If you just have to do something, do something do not let any result answer to your browser, what should I do?
You don't have to be afraid, here is here to explain these situations.
Use another text as a response
The CGI output is not a data stream, sometimes telling the browser to be a page on the server, in order to issue this information, look at the following example:
Location: ../docs/final.html This Location row is used as a usual output location, that is, if you use location, you don't have to use data like Content-Type (actually, you can't ). As Content-Type, you must also follow up with an empty line behind this line.
Point to this file can be a URL or relative path. All relative paths refer to the location of the script. The final.html text in the example is in the current directory DOCS directory:
echo location: ../docs/final.html
echo
You can't use both outputs simultaneously. For example, if you want to output a standard page, you want to add the content of the customer in this page, you have to build these two parts yourself with the Content-Type. Note : You can use the script command to open a local file as the data directly.
No response
Sometimes there may be no output for a CGI script. Sometimes you just want to collect some information from the user. You don't have to call a new text, nor you need to output the result or open an existing file. On the browser, the screen is still .
Fortunately, it's easy. You just want to output the following command (back with an empty line):
Echo Status: 204 no response
Echo This Status head provides a status code to the server (and also gives the browser). Special 204 will pass to the browser, if it can identify it, it will not do anything. Although there is no response is part of an official HTTP. But not suitable for all browsers, may have a strange result, then you have to try it more.
Handling the script of the form
Today, most CGI scripts are used to handle form input. This process is roughly the same, but still somewhat different, such as CGI scripts as long as they are called; how data is sent from the server to the browser.
Remember, most forms have two parts: HTML's form format; CGI script for processing form data. This CGI script uses tag