Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CGI script to generate web pages

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use CGI script to generate web pages, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The Universal Gateway Interface (CGI) provides an easy way to generate dynamic Web sites in any language.

Back to the beginning of the Internet, when I created my commercial website for many times, life was so good.

I installed Apache and wrote some simple HTML pages that listed some important information about my business, such as a product overview and how to contact me. This is a static site because the content rarely changes. Since the content of the site rarely changes this nature, it is easy to maintain.

Static content

Static content is simple and common. Let's quickly browse some examples of static web pages. You don't need a runnable website to perform these little experiments, just put the files in your home directory and open them using a browser. What you will see will be the same as what you see in this file provided through the Web server.

For a static website, the only thing you need is the index.html file, which is usually placed in the / var/www/html directory. The content of this file can be very simple, for example, it can be a short text like "Hello, world" without any HTML tags. It will simply display the contents of the text string. Create an index.html file in your home directory and add "hello, world" as content (no quotation marks are required). Open this file in your browser with the following link:

File:///home//index.html

So HTML is not necessary, but if you have a lot of text that needs to be formatted, the results of pages that are not encoded by HTML will be difficult to understand.

So the next step is to provide formatting by using some HTML encoding to make the content more readable. The following command creates a page with the absolute minimum markup required for a HTML static page. You can also use your favorite editor to create this content.

Echo "Hello World" > test1.html

Now, if you look at the index.html file again, you will see that it is a little different.

Of course, you can add a large number of HTML tags to the actual content lines to form a more complete and standard web page. A more complete version is shown below, although the same content will be seen in the browser, but it also lays the foundation for a more standardized site. Continue to write these in index.html and view them through the browser.

My Web PageHello World

I have used these technologies to build some static websites, but my life is changing.

Dynamic web page

I found a new job where the main task is to create and maintain CGI (Common Gateway Interface Common Gateway InterfaceM) code for a dynamic website. Literally, dynamic means that the HTML required for a web page generated in a browser is generated by different data each time the page is visited. This data includes user input in the web form for data lookup in the database, and the resulting data is surrounded by some appropriate HTML and displayed in the requested browser. But it doesn't have to be very complicated.

By using CGI scripts, you can create simple or complex interactive programs that can generate dynamic pages based on changes in input, calculations, server current conditions, and so on. There are many languages that can be used to write CGI scripts, and in this article, I will talk about Perl and Bash, and other very popular CGI languages include PHP and Python.

This article will not cover the installation and configuration of Apache or any other web server. If you have access to a Web server where you can experiment, you can directly view the results they appear in the browser. Otherwise, you can run programs on the command line to view the HTML text they create. You can also redirect the HTML output to a file and view the result file through a browser.

Use Perl

Perl is a very popular CGI scripting language, and its advantage is its strong text manipulation ability.

To make the CGI script executable, you need to add the following line to the httpd.conf of your site. This tells the server the location of the executable CGI file. There is no need to worry about this problem in this experiment.

ScriptAlias / cgi-bin/ "/ var/www/cgi-bin/"

Add the following Perl code to the file index.cgi, which should be placed in your home directory in this experiment. If you are using a Web server, change the owner of the file to apache.apache and set the file permissions to 755, because it must be executable no matter where it is located.

#! / usr/bin/perlprint "Content-type: text/html\ n\ n"; print "\ n"; print "Hello World\ n"; print "Using Perl

\ n "; print"\ n "

Run the program on the command line and view the results, and it will show the HTML content it generated

Now, when you look at the index.cgi file in a browser, all you see is the contents of the file. Browsers need to treat it as CGI content, but Apache does not know that this file needs to be run as a CGI program unless the configuration of Apache includes the ScriptAlias definition shown above. Without this configuration, Apache simply sends the data in the file to the browser. If you have access to the Web server, you can put the executable in the / var/www/cgi-bin directory.

If you want to know what the script looks like in the browser, rerun the program and redirect the output to a new file with any name you want. Then use the browser to view the file, which contains the content generated by the script.

The above CGI program still generates static content because it always produces the same output. Add the following line to the line "Hello, world" in the CGI program. The system command of Perl will execute the shell command that follows it and return the result to the program. At this point, we will get the current memory usage through the free command.

System "free | grep Mem\ n"

Now, rerun the program, redirect the results to a file, and reload the file in the browser. You will see an additional line that shows the memory statistics of the system. Run the program several times and refresh the browser, and you will find that memory usage should be constantly changing.

Use Bash

Bash is probably the simplest language used in CGI scripts. The advantage of using Bash for CGI programming is that it has direct access to all standard GNU tools and system programs.

Rename the existing index.cgi file to Perl.index.cgi, then create a new `index.cgi file and add the following. Remember to set permissions to make it executable.

#! / bin/bashecho "Content-type: text/html" echo "echo" 'echo', 'echo', 'echo' Hello World'echo', 'echo' echo 'Hello World

'echo 'Using Bash

'free | grep Memecho' 'echo' 'exit 0

Execute the file on the command line and view the output, then run it again and redirect the results to a temporary results file. Then refresh the browser to see what the web page it shows looks like.

The above is all the content of the article "how to use CGI script to generate web pages". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report