![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
I need a little help to get started on CGI scripting
I'm a terrible novice at this, I only started getting interesting yesterday. So even if my question sounds stupid, try to answer it if you can.
Now, I have a background in static HTML and the C language. I understand CGI scripts are pieces of code written in Perl or actual executables to be run from the server. Now, if I compile a C program and I place it in the cgi-bin folder of the server root, why does the server not "execute" it? I tried something very simple, a file named "database.txt" and each time the script was supposed to be lannched it would modify the file somehow (i.e. add a character to it). I'm running the Abyss Web Server to test the future site, but it doesn't want to run the executable. (I attempted to install Apache yesterday, but I gave up after 2 hours of tinkering with it - why doesn't it come in Windows friendly?). Well Abyss thing runs static javascript et. all, but I can't get it to run cgi although it should according to the specifications. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
I actually managed to compile a script to do something, yet it can't open files. Any help would be most welcome
Here is the source of the test program I compiled: #include <stdio.h> #include <stdlib.h> main() { FILE *f; f=fopen("http:\\\\127.0.0.1\\cgi-bin\\database.txt","a"); if (f==NULL) { printf("Content-type: text/html\n\n"); printf ("<p>ERROR!</p>"); } fprintf (f, "%c ", 'X'); fclose(f); } This outputs "ERROR!" all the time and the file is not modified (although it exists at that location) |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You want forward-slashes, mate.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
Nope, still doesn't work. I removed the slashes altogether and placed the file in the same folder as the cgi. So how does file management work (preferably with C)?
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
f=fopen("http:\\\\127.0.0.1\\cgi-bin\\database.txt","a");There's your problem. I know the backslash/forwardslash thing has been sorted out, but there's more trouble here than that. fopen()'s first argument is supposed to be a filename, not a URL; to open a URL takes a lot more work (with sockets) and isn't what you want to do here anyway. If the file is in the same directory as the CGI program, open it with: f = fopen("database.txt", "a");If you can't open for writing, check to make sure the file (database.txt) isn't set read-only. It might also be worth getting your CGI program to check if its present working directory actually is the program it's in - this isn't always true for CGi scripts, which sometimes run with root (/) as their PWD, in which case you will have to give the full path. I haven't tested this snippet, but it should work: #!/usr/bin/perl -wT
print "Content-type: text/plain\015\012\015\012";
print "Document Root: ".$ENV{'DOCUMENT_ROOT'}."\n";If you put this in your cgi-bin and make sure it's set executable (and possibly that it's saved with a .pl or .cgi extension), it'll tell you your document root. On my development machine, this gives me /var/www/htdocs (it's a Slackware box with Apache). What I really think you need, though, is a good book on CGI. Did you know that the arguments are sent as environment variables to the script? That POSTed form data can be read from the CGI script's standard input? You're also best off with a good CGI library. I've never actually done any extensive CGI programming in C so I don't know of one for C. If you're using Perl, the CGI.pm Perl module is great. These libraries help make it easier to access your arguments, receive multipart uploads, and they'll even often help generate HTML code so you don't have to include it in your program, which can be a bit laborious. Finally, CGI programming is Unix programming. Unix is the operating system of the internet. I don't care what anyone may say about market share; there's a reason why the slashes in URLs face that way. So you might find Eric S. Raymond's "The Art of UNIX Programming" a nice read. Hope this helps! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|