Can a CGI program tell how it was started?

Discussion in 'Rebol' started by swhite, Jun 5, 2012.

  1. swhite

    swhite Member

    I have a simple CGI program that accepts data from a form does stuff with it. Of course it crashes the first time, and the server error message is not helpful. It is run by means of the line at the front:

    #!c:/rebol-sdk/tools/rebcmd.r -cs

    A CGI program can be run from the REBOL console, but the input form is not available with that approach.

    What I want to do is put code in the program that says, in effect:

    IF I was started by a web browser THEN
    accept data from the html form
    ELSE
    ask for the data items from the console

    This would allow me to run the script "off-line" so to speak and see the error messages.

    The question is, is there something available in REBOL that I can use to detect if the program was run through the console or through CGI?

    Thank you.
  2. Graham

    Graham Developer Staff Member

  3. swhite

    swhite Member

    either system/options/cgi/remote-addr [
    RUN-AS-CGI: true
    ] [
    RUN-AS-CGI: false
    ]

    Thank you. Works like a charm.
  4. Graham

    Graham Developer Staff Member

    If that works, this will work better:

    Code:
    run-as-cgi: system/options/cgi/remote-addr
    
  5. swhite

    swhite Member

    Now that is interesting. If I did not finally know that 1) remote-addr is the address of the calling computer, 2) that remote-addr has a value of "none" if there is no calling computer, 3) that a value of "none" returns "false" in a test with the "if" function, and 4) that a non-none value returns a value of "true" in an "if" test, I would find your line of code hard to understand. That might explain, partly, why I did (and sometimes still do) find REBOL hard to understand.

    Also, it seems that your one line is a "REBOL-ish" way of coding, whereas mine is a more "COBOL-ish" way. That suggests to me that the tools we are used to using can influence our thinking, and sometimes trap us in certain ways of thinking.
  6. Graham

    Graham Developer Staff Member

    Well, if you know what are truth values, then you can do the assignment directly rather than testing twice. This is just a form of code factoring.

Share This Page