Cgi bin txt


















I've installed both Perl and Python on my server - I have complete control of my server - and found that I don't have to use a shebang or anything - this suggests the files can be executed on any folder in the server.

If I'm remembering right, the shebang points to the executable for Python or Perl, right? I guess the two questions are linked. Hope you understand the questions.

The "cgi-bin" is a "Linux" traditional concept for using Perl scripts to provide security on "Apache" Web Server. To use perl scripts the execution is limited to the "cgi-bin" directory when launched from "Apache" Web Server.

The "cgi-bin" directory is always isolated away from the the Web Server and the file system. You create an alias "cgi-bin" in the web server to point to the real location ie. You really should use the shebang in perl scripts! A file is locked after you open it because the filehandle doesn't exist before you open the file :.

Keep in mind that file locking is only effective if all of the programs that read and write to that file also use flock.

Programs that don't will ignore the locks held by other processes. Since flock may force your CGI program to wait for another process to finish writing to a file, you should also reset the file pointer, using the seek function:.

So seek OUTF,0,2 repositions the pointer to the end of the file. If you were reading the file instead of writing to it, you'd want to do seek OUTF,0,0 to reset the pointer to the beginning of the file. Files are automatically closed when your program ends. File locks are released when the file is closed, so it is not necessary to actually unlock the file before closing it.

In fact, releasing the lock before the file is closed can be dangerous and cause you to lose data. There are two ways you can handle reading data from a file: you can either read one line at a time, or read the entire file into an array. Here's an example:. If you were to use this code in your program, you'd end up with the first line of guestbook.

The following section of code shows how to read the entire file into an array, then loop through each element of the array to print out each line:. This code minimizes the amount of time the file is actually open. The drawback is it causes your CGI program to consume as much memory as the size of the file. Obviously for very large files that's not a good idea; if your program consumes more memory than the machine has available, it could crash the whole machine or at the very least make things extremely slow.

To process data from a very large file, it's better to use a while loop to read one line at a time:. Let's try another example: a web poll. You've probably seen them on various news sites.

A basic poll consists of one question and several potential answers as radio buttons ; you pick one of the answers, vote, then see the poll results on the next page. In this example we're using abbreviations for the radio button values.

Our CGI program will translate the abbreviations appropriately. Now the voting CGI program will write the result to a file. Rather than having this program analyze the results, we'll simply use a redirect to bounce the viewer to a third program results. That way you won't need to write the results code twice.

Finally results. The results program only shows the total number of votes. You may also want to calculate the percentages and display a bar-graph for each vote relative to the overall total.

We'll look at how to calculate percentages in the next chapter. If, after reading about suexec , you still wish to use it, then run suexec -V to find the location of the suexec log file, and use that log file to find what policy you are violating. As you become more advanced in CGI programming, it will become useful to understand more about what's happening behind the scenes. Specifically, how the browser and server communicate with one another.

Because although it's all very well to write a program that prints "Hello, World. Environment variables are values that float around you as you use your computer. They are useful things like your path where the computer searches for the actual file implementing a command when you type it , your username, your terminal type, and so on. For a full list of your normal, every day environment variables, type env at a command prompt. During the CGI transaction, the server and the browser also set environment variables, so that they can communicate with one another.

These variables are available to the CGI programmer, and are half of the story of the client-server communication. This simple Perl CGI program will display all of the environment variables that are being passed around. Two similar programs are included in the cgi-bin directory of the Apache distribution. Note that some variables are required, while others are optional, so you may see some variables listed that were not in the official list.

In addition, Apache provides many different ways for you to add your own environment variables to the basic ones provided by default. The program then can process that data as though it was coming in from the keyboard, or from a file.

The "special format" is very simple. Inconvenient characters like spaces, ampersands, and equals signs, are converted into their hex equivalent so that they don't gum up the works. The whole data string might look something like:. You'll sometimes also see this type of string appended to a URL. That's called a GET request. Your program is then responsible for splitting that string up into useful information.

Fortunately, there are libraries and modules available to help you process this data, as well as handle other of the aspects of your CGI program. When you write CGI programs, you should consider using a code library, or module, to do most of the grunt work for you. This leads to fewer errors, and faster development. The most popular module for this purpose is CGI. You might also consider CGI::Lite , which implements a minimal set of functionality, which is all you need in most programs.

If you're writing CGI programs in C, there are a variety of options. When you post a question about a CGI problem that you're having, whether to a mailing list, or to a newsgroup, make sure you provide enough information about what happened, what you expected to happen, and how what actually happened was different, what server you're running, what language your CGI program was in, and, if possible, the offending code.

This will make finding your problem much simpler. Note that questions about CGI problems should never be posted to the Apache bug database unless you are sure you have found a problem in the Apache source code.

Copyright The Apache Software Foundation. Licensed under the Apache License, Version 2. What's going on behind the scenes? Note: If Apache has been built with shared module support you need to ensure that the module is loaded; in your httpd. But it's still not working!

There are four basic things that you may see in your browser when you try to access your CGI program from the web: The output of your CGI program Great! That means everything worked fine.

If the output is correct, but the browser is not processing it correctly, make sure you have the correct Content-Type set in your CGI program.



0コメント

  • 1000 / 1000