The Role Of Common Gateway Interface

“An interface between programs outside the HTTP server and the HTTP server itself”

Common Gateway Interface, or CGI, is something quite often referred to in forums and on lists and I notice there is a tendency for even some highly skilled people out there, people I look up to in fact, to call anything that works on the server side as CGI. I’m sorry but its not and I’ll hopefully explain to you why they are a little off track. Specifically when they say that PHP is CGI - its not.

I don’t want to complicate this by running off on tangents so this is a very basic explanation. Common Gateway Interface - consult the diagram below - is a way for scripts or programs to interface with the server. Its an agreement between the HTTP Server and your program. CGI is how the information is transferred to the program and how the program responds to the server. The HTTP Server makes environment variables and passes values to the program.

CGI works as a separate process and is more dangerous in some cases such as with deletion of files on multiuser systems.

Also CGI has the disadvantage that if 100 users call the program at the same time you need 100 processes running, each a temporary child process - the server uses timesharing so the computer slows and the wait gets longer. Whereas PHP actually uses only 1 process because its integrated into the HTTP Server and in that way is more efficient. PHP is actually a part of the server process.

Anyway the diagram should give you the basic idea I guess. PHP is integrated as a module into the server software whereas CGI programs are external to the server which communicate via the Common Gateway Interface. Whereas with PHP some headers are generated for you and strings are preprocessed its different with CGI which requires you to put headers in your program.

Don’t understand this as a PHP versus CGI thing because its not that easy. In some cases PHP is better and in others CGI scripts and it depends on the deed to be performed and the skills of the programmers. CGI can be programmed in any language, either compiled languages or interpreted, and has obvious advantages right there. Java for instance is more efficient than PHP in one sense as it doesn’t have to reload into memory so while the first process is slower the second is faster.

Anyway this is way beyond the scope of what I wanted to point out. Simply that PHP is not CGI and I think its detrimental that some high profile gurus keep insisting that PHP is CGI for some reason just because it runs over there on the mysterious server somewhere. Common Gateway Interface is specifically an interface between programs outside the HTTP server and the HTTP server itself.

I hope this hasn’ t been too long and overly complex, I’ve culled it down to the minimum at short notice. The important points are easily gleaned from the diagram below. I’m no back end gun guru programmer but I think its important to conceptually understand what is happening on the server for any budding developer to make good product. There’s no magic and it shouldn’t be that way in developer’s minds. Note how PHP is located in the server and CGI programs are not.

When I say server I don’t mean the physical server box so don’ t be confused. I mean the HTTP Server such as Apache.

Common Gateway Interface is external to the HTTP Server as described in the article

8 Responses to “The Role Of Common Gateway Interface”

  1. Felius Says:

    Even this is a bit of a simplification :) It’s quite possible to run PHP programs as CGI scripts - this is how it was done before the Apache module came around, and it used to be done this way on IIS servers for quite some time until they got their own ISAPI module.

    Likewise, Perl, Python, C, and Java programs can often use a different interface to talk to the server, meaning that they’re not CGI programs either.

    And finally, your webserver may actually be written in the language you’re using - there’s nothing to stop you writing a PHP application which acts as a web server (honest!). This is actually fairly common place with some other languages such as Python (for example, applications based on Zope or Twisted).

    So, umm.. yeah. Not trying to tear you down or anything.. guess this is just my funny way of saying hi to another Tasmanian blogger ;)

  2. nortypig Says:

    Hi Felius
    Ha ha cheers mate… yeh I was tempted to link to something about running PHP as a CGI but thought that would overly confuse the message. That being what CGI does in a very basic sense I guess - interface.

    The context I was specifically talking about is where quite a number of front end coders lately seem to be talking generically about Common Gateway Interface as anything running on the server… as in all PHP is CGI… or that C “IS” CGI, or any program that is up there. I’m trying to point to the CGI bit as opposed to the program that interfaces I guess. But yeh its a simplification for sure. With an article one has to pull out the message and try not to run full page articles if possible with all the complexities. Also, its only a conceptual model I suppose so it has its limitations :)

    I also was aware of the server part but its interesting to hear Perl, Python, C and Java can use a different interface. Cool. I’m learing C first semester this year at Uni and have done some Java.

    Another thing I skipped was the role of middleware in the diagram I suppose lol, and a big heap of stuff about who owns CGI files versus PHP module files etc…

    Anyway Felius interesting stuff, thanks for filling us in. Nice blog you’ve got over there by the way… theme or custom? I’m an embarrassing Kubrick at the moment as you can see lol. Ha don’t laugh :)

    Yeh its OK to tear me down… the thing is I’m not actually always right. I’ve had some major faces come by and clip my wings over the time for sure. Cheers mate - another local is always welcome.

  3. Felius Says:

    Well, the core of what you were saying is absolutely correct - “Don’t say CGI if you really mean Server-Side Processing”. It’s difficult to approach complex subjects like this in a simple fashion, and my hat goes off to you for even attempting it. In fact, I’m amazed at the number of posts you’re able to churn out! Just a more motivated and disciplined writer than me, I guess.

    With the server interfaces for other languages, I was mostly thinking about things like mod_perl and mod_python which effectively do the same thing as the Apache PHP module but for those languages instead.

    But if you want to write something in C you can always hook into Apache directly with your own module, using their server API!

    I’m using the Green Marinee theme on my blog, which I found here. I stuck with Kubrick for almost a year though, so don’t feel bad. ;) I’m not much of a graphic designer myself, so I’m talking to a mate of mine who may be able to help me out in that department.

  4. nortypig Says:

    Hi Felius
    Ha yeh I’ve been Kubrick for about 3 weeks i think. I had WP 1.2.2 and needed an update so some hacker made short work of me. I was busy with uni so upgraded and stuck with Kubrick… ha. Bastardo hacker!!!

    Anyway my old blog designs for here and Pig Pen are the bottom two on my Portfolio page. Not great but a lot warmer than this… so its a bit weird being Kubrick.

    K gotta go pick the boy up from surgery - cut his hand at work today a beauty… microsurgery. Cheers mate.

    O do you work? Experience with Perl?

    Ha I’ll be posting less frequently soon because of the workload at university and Norty Pig. Cheers.

  5. Felius Says:

    Sorry to hear about your boy, I hope it all works out ok. My son hasn’t managed to cause himself any serious injuries yet - but he’s only 10 months old, give him time. ;)

    Your portfolio page shows some really nice work!

    I work full-time, and I have experience with Perl. My perl is a bit rusty these days and it’s several years since I was paid to do any perl work, but I may be able to help depending on what you had in mind and how much work it is. Drop me an email if you want to chat about it :)

  6. nortypig Says:

    Hi Felius
    Ah Anthony is 17 and a first year renderer apprentice! Kids that age are born to come home with injuries… he had a 40 kilo bucket of render in his left hand going up a ladder and the end of the bucket handle came out, then the end buried in his hand and ripped before hanging there. When he got to the top he noticed the blood. Mmm is fine though, just stretched a nerve and needed stitches.

    Like many portfolios its getting a bit dated now but oneday I may meet up with a decent graphic artist and start producing something a bit less minimal and a bit slicker. Touch wood. Thanks for the compliment.

    Mmm perl, we were trying to figure on a good Perl programmer not long ago to do with a legacy system but they are a bit thin on the ground nowdays. It was quite a big job though… will keep you in mind for work to pass on though if any more comes by here, it occasionally does. Do you have your resume online?

    I’ve gotta run and prepare a presentation for friday… oh my fave thing! Cheers mate and thanks for getting back…

  7. Felius Says:

    That sounds like a nasty cut. :(

    Good Perl programmers are a bit rare these days - I only know two personally, but they’re both very good. Your best bet might be to try someone like the Melbourne Perl Mongers.

    I don’t have my resume online - I only tend to update it once a year at most ;)

  8. nortypig Says:

    Hi Felius
    ha i must have missed something … nasty cut???

    Thanks for that link I’ll pass it on. I’m not sure they could find any decent perl programmers and in all honesty I think they’re far better breaking from the legacy into developing new anyway. But that link will be handy, thanks.

    Mmm I guess if you’ve got a job already its probably not that important having one online anyway lol… cheers mate… have lots to do tonight on this end… :)