⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 如何用c语言写cgi(2).txt

📁 C&C++论坛精华
💻 TXT
字号:
作者:hooke
日期:2001-2-28 0:35:44
After this call, VarOne will contain the value for your variable, var_one. The function get_var also decodes hex via another function, decode_hex(). You don't need to call decode_hex directly or worry about it at all. What it does do, though, is quite useful. For example, on my guestbook, you can enter HTML comments and decode_hex() ensures that those comments will appear using proper HTML. Try it out. Add a link to your site, or a graphic at your site, etc. So, you don't need to worry about special characters.

What about checkboxes??!
How do you handle checkboxes? Remember that checkboxes take on the form of 
...&chbx=on&...
inside the stream from stdin. However, if the checkbox is unchecked, nothing appears in stdin! Here's a way to handle checkboxes. On failure, get_var returns -1 (negative one). On success it returns an integer corresponding to the length written to dest. You can use this to check if the name of a checkbox appears in the stream. If it does appear, you know the box was checked, otherwise the box was not checked. I recommend that you declare a checkbox dummy array which needs to be long enough to hold the possible value of a checkbox variable (i.e. 3 characters o, n, and \o). You can re-use this for all your checkbox calls. Get your real information from the return value of get_var -1 or some positive number:


if ((get_var(stream, chk_bx, "CheckboxOne="))== -1)
{
box_one=0; // it's off!
}
else
{
box_one=1; // it's on!
}

Special thanks to Michael Graessle for noting that printf() will print an entire string and will not break because of embedded spaces. scanf() will break, but printf() will not. For example, if you have read in a text area or input line and now have a string like "this only yours" you can print it to a file or send it to the user with fprintf() or printf(). 
Good luck and Happy CGIing!
I hope that this helps someone out there. If you have any comments regarding this tutorial, please send them to me. I'm interested in any response I get about this page. bsears@physix.com



CGI FAQ
I am trying to run a script from your CGI in C page and it won't work, why? Do I need to run it on the server?

You will have to test most of your scripts on the server. There are a number of reasons for this. First, if your script accesses environmental variables, they won't be properly in place if you are just trying to run them on your local PC or Mac (or even Unix workstation) and not on a server. Also, if you are processing form data sent as a POST, stdin will not be set up properly. In fact, stdin is usually the keyboard. The output from your script will just show up on the screen rather than being sent to your browser. That can also cause problems in debugging scripts. Also, the idiosyncracies of your server are probably different than your own PC or Mac or whatever you have. If you configure your computer that you are writing on to act as a server for local files, then you can test your scripts on your local machine. Unless you already know how to do this, it is probably more trouble than it is worth. I recommend debugging on your server. However, if you are lucky enough to own a development kit (i.e. a C compiler or some type) I do recommend at least having the compiler check the syntax or precompiling the code to save time. It will catch silly errors like leaving off a ; or similar errors.


I can't tell if the problem is that my script is not set up right, the server isn't set up right, or if I am not calling my script with the right URL. How can I tell which it is?

Before you write a complicated script, you should determine the following things

You can run CGI on your account 
You can write a CGI in C language and have it work on your account 
You know what URL to call to get a script to run on your account 
A good test for this is to run a very simple Hello, World script on your account first. Here's source code for such a script:

#include <stdio.h>
#include <stdlib.h>

main()
{
printf("Content-type: text/html\n\nHello, World!\n");
return(0);
}

If you can get this to work, you know that any problems after this are your own. If you cannot get this script to work contact your system administrator/provider. I can't help because I don't know anything about your system.

How can I send email from a CGI script? I want to make a fill out form and have information sent to me. (I want to run some external process from my CGI script.)


If your server is not a Unix server, stop reading. I can't help you. If it is, read on for a solution that works on my server, and probably will work on yours, too. Sending email basically involves starting a second process (i.e. running a program from inside a program). There is an easy way to do this in C provided that the program you are starting doesn't need any help after you get it going (i.e. a non-interactive process). You can do this with a system() call. This sends one line to the Unix shell. There is a way in C to open an interactive process using the popen() command. I know that it can be done, but it is easier to use system() and run a non-interactive process if it is at all possible to do so in your situation. You can use this command to send a file from the unix shell:

mail -s "Subject" someone@somewhere.domain This sends thefile.dat as the body of the message. Subject is the subject and the message is sent from you to someone@somewhere.domain. (Always remember that the message is from you - don't allow people to send threatening messages to the university president because the return address is you.) You already know that you can open a file from inside your script and print to that file. The solution is to use a scratch file. You print the info into the scratch file, mail yourself the scratch file, and delete the scratch file. Here's some code:

main()
{
FILE *out;

out=fopen("temp.dat", "w"); //opens the file

fprintf(out, "This is the body of the test message"); //prints to the file

fclose(out); // closes the file
system("mail -s \"test message\" you@server.domain <temp.dat"); //sends the mail

system("rm temp.dat"); // deletes the scratch file
return(0);
}


That's all folks! I hope you like this page. bsears@physix.com

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -