Department of Computing Imperial College
Q&A on CGI and PHP

More notes in the CGI and PHP guides.

What is CGI and how does it work in DoC?
My cgi script gives "server error 500" and "malformed header in script".
Why does my cgi script not write to a group directory?
Is there any reason why I can't run cgi scripts written in python?
Can a CGI script send mail?
Can php scripts run without being world readable?
Is it possible to set index.php as an index file?
Can I use cgi with https?

My cgi script gives "server error 500" and "malformed header in script". Top of page

This particular error message is generated if the CGI script fails to output a valid header line when it runs.

A few things to check out...

Is there any reason why I can't run cgi scripts written in python?

The web server executes any file ending in '.cgi', whether it is written in perl, python, assembler, COBOL, or whatever.

Any further diagnosis depends on the script itself.

Why does my cgi script not write to a group directory?

CGI scripts run from directories with a /~user/ are run as that user. Scripts run from other directories are run as the unprivileged Apache user 'wwwnot'.

The script in ~user runs as you, hence it can write to areas you have write access to. The one in the group directory runs as wwwnot and has no write permission anywhere.

It is very hard to provide secure access to non-home directories, as there is rarely a single user responsible. The web server does not yet understand the concept of groups well enough to provide the same kind of access to shared directories as it does for home directories.

The easiest way to do what you want is to keep the dynamic content in your home directory (possibly symlink to it from the group directory), so the script runs as you.

Can a CGI script send mail?

Yes, /usr/sbin/sendmail (a link to exim, the MTA we use, sendmail itself is not installed anywhere) exists on all our machines, and accepts sendmail style arguments to send mail. Alternatively, if your CGI script is in perl, the 'Mail::Send' module is installed and works fine.

See: ~dcw/src/perl/mail/testsendmail.pl for an example of calling sendmail direct, and ~dcw/src/perl/mail/testmail2.pl for an example of using Mail::Send.

Can php scripts run without being world readable?

Yes they can. If these files are inside your public_html directory, change the permissions to be 700:

chmod 700 blah.php
Then, at the top of the script place the following command:
#!/usr/bin/php
There you go, all done!

Is it possible to set index.php as an index file?

You can override the default "index.html" setting (and many others) by creating a ".htaccess" file in the directory. Complete Apache documentation is available but you probably just want:

DirectoryIndex index.php

Can I use cgi with https?

Sorry no - it doesn't work with the current version of the web server software.

(04/02/05)

© CSG / 2005