No announcement yet.

CGI Tutorial Part 2

  • Filter
  • Time
  • Show
Clear All
new posts

  • CGI Tutorial Part 2

    In the process of developing a new web application, I decided to take a quick break and update the CGI Programming Tutorial I wrote a few years back. Instead of posting Part 2 here, I opted to put it up on my web server for now.

    If anyone is interested, parts 1 and 2 can be viewed at:

    I will be adding to it on a regular basis over the next few months, so if you have any comments, questions, or suggestions, please post them here.

    Just added page 3, 'Using Cookies'

    Last edited by Joe Byrne; 21 Dec 2007, 12:07 AM.
    Software makes Hardware Happen

  • #2

    Hello Joe!

    I appreciate you're posting of the information Joe, cuz I don't know very much at all about it and I'd like to learn sometime soon. I just briefly looked over your lessons and what I'm wondering is if it would be possible to somehow set up a stand-alone computer to function as both the client and the server (I hope I'm using the right terminology) to practice and learn CGI programming with PowerBASIC? Where I'm comming from is a year or two back I installed Internet Information Server on a couple of my laptops, and I also have an old version of .NET (2002 I believe, actually Visual Studio.NET), and with that setup in conjunction with ASP.NET (if I recall correctly) I was able to teach myself a little bit about internet programming.

    Also at that time I installed another Microsoft product named the 'Web Matrix Project', and that too allowed one to learn this style of coding in the absence of getting web servers involved.

    For me (and I expect for others too), this might be an issue because with my poor little dial up connection to the internet which costs me $10/month my ISP isn't likely to let me run executables off their server I don't imagine!

    So while I played around with internet programming a little bit using the MS tools, I kind of lost interest because I don't care for MS's programming tools very much. However, PB and CGI is another story. What do you think? Is there a way to set any of this up on a stand alone computer just for learning purposes?


    • #3

      Absolutely! The web server can run anywhere you like, on a dedicated server, or on your own PC.

      I would highly suggest you download the free version of Abyss and install that on your PC. First, it works great for PB CGI apps and second, its probably the easiest to install and configure. Pretty much right out of the box.

      For anyone interested, here are a couple of macros I use to make getting the values from my forms a bit easier:
      '---HTTP Form Macros---
      MACRO lCGIField(cgiField) = TRIM$(LCASE$(CGIPARAM(buffer,cgiField))) 'String form data - LowerCase
      MACRO uCGIField(cgiField) = TRIM$(UCASE$(CGIPARAM(buffer,cgiField))) 'String form data - UPPERCase
      MACRO sCGIField(cgiField) = TRIM$(CGIPARAM(buffer,cgiField))         'String form data - Normal Case
      MACRO nCGIField(cgiField) = VAL(CGIPARAM(Buffer,cgiField))           'Numeric form data
      Also, this little ditty strips out all the name=value pairs from a web form and sends the data back to a new page. Its a handy way to verify what form control names you've used, how they're spelled, and whether or not the data is actually there:
      Buffer$ = READCGI
      Count& = ParseCount(Buffer$,"&")
      REDIM Params$(1 to Count&)
      Parse Buffer$,Params$(),"&"
      For i& = 1 to Count&
           WriteCGI Params$(i&) & "<BR>"
      Software makes Hardware Happen


      • #4
        Tutorial #1

        “I will be using pb/cc for the cgi application mainly because the console compiler has the necessary i/o capabilities built in, but pb/win is also capable of creating the necessary code. If you need to use pb/win, you should download the file from the pb site here: ...”
        If you use PB/CC you will still need to use to run Joe’s example. This include file came with PBCC and in the standard installation can be found in the folder C:\PBCC40\Samples\Internet\CGI
        Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.


        • #5
          Sounds good. I'm going to take time to study up on Abyss and so on. Not sure that I want to be the last person on Earth writting non-internet apps.


          • #6
            Thanks Joe!

            The CGI tutorials are well written and very informative. I don't know squat about Internet programming (but am interested in learning) and I found the tutorials a great place to start.

            VERSION 3 IS COMING SOON! Our developers have been hard at work on a new version of Liquid Studio! Look for the new release coming on October 1, 2021!   VERSION 1.01 HAS BEEN RELEASED! Liquid Studio...


            • #7
              Yes, thanks for the cookie examples, any cookie example you can put up is welcome to me

              In fact anything we usually not see... like all other stuff in the http headers

              Suggestion: FTP ?


              • #8
                Suggestion: FTP ?
                You bet! I do a lot of FTP and have used a great many examples and tools from this forum. Don's worked ok for small stuff, but quickly ran out of steam. After much research and testing, I can say without hesitation that the only FTP code I use is:

                Ok, I haven't played with the routines in Socket Tools yet but based on the rest of the package, I'd guess they are pretty good. However, I've yet to be disappointed with any part of Marshallsoft's routines and not being one to remake the wheel, its about as far as I need to go
                Software makes Hardware Happen


                • #9
                  Part 4 Now Available

                  I just posted part 4 of my CGI Programming Primer for those interested.
                  Software makes Hardware Happen


                  • #10
                    Thanks again, Joe. I was working on it trying to learn the stuff you presented, as well as reviewing a very, very introductory ASP.NET book I have, temporarily sidetracked! Will get back to it soon I hope. I did manage to get the Abyss Web server installed locally (as you recommended) on one of my computers, and was in the process of studying up on those docs. I need to pull it all together.


                    • #11

                      Allows using PBWIN with cgi programs.


                      • #12
                        Great tutorials!!


                        • #13
                          Joe, I'm trying to learn the cgi technology that you are illustrating in Part 1 of your cgi tutorial. I have web space from Verizon, allocated as part of my contract making them my ISP. After trying to implement your example, I'm getting the following error message:

                          The requested method POST is not allowed for the URL /chasdietz/cgi-bin/contact.exe.

                          Does that mean what I'm afraid it does... that I cannot use cgi on the web space they've provided?

                          My website address is:

                          Thank you for whatever help you have time to give me. I sure do appreciate your cgi tutorial. I have wondered a long time what exactly cgi was.


                          • #14
                            their server is Linux or Unix so your contact.exe won't run on it..... at least until pbLinux comes out


                            • #15

                              As Shawn pointed out, PB/CGI applications only run on Windows based Web servers. (However, if you have access to a Windows PC/server, you can separate the web pages from the CGI application. The HTML files and CGI programs do not need to run on the same physical machine. You would need a static (fixed) IP address to your windows box, or use a service that routes your dynamic address.....but I may be getting ahead of myself here

                              There is a lot you can do with Web apps, but in actuality, they are not all that different than any other PB application. I'm just putting the finishing touches on a rather large reservation system that is made up of 8 web pages (to gather all the necessary data), pulls data from 12 separate Tsunami data files, and in the end updates all 12 (over 25 "writes" not including various reads and numerous calculations and encryptions), then generates a 4-page PDF report with graphics, tables and loads of color, all from one PB/CC program. You just need to get used to the 'back-and-forth' nature of the web.

                              Load a page, enter data, run your CGI.
                              "" "" "" "" "" "" "" ""
                              "" "" "" "" "" "" "" ""
                              "" "" "" "" "" "" "" ""
                              "" "" "" "" "" "" "" ""

                              Of course there are lots of other things you can and usually do to make the process faster. That's where javascript and sometimes PHP/PERL come into play. PHP is fast, lots of pre-written functions floating around, and very portable. But so far, I have yet to find anything done in PHP that I can't do faster and easier (IMO) using PB.
                              Software makes Hardware Happen


                              • #16
                                Originally posted by Edwin Knoppert View Post
                                Suggestion: FTP ?
                                FTP can be a complicated protocol, particularly if you want broad compatibility with a lot of different servers. I know that in SocketTools, we have a lot of code that is dedicated to working around issues with different types of servers. Not all servers support the same command sets, and not all of them have commands that behave in the same way (the STAT command is an example of that). Throw in things like proxies and firewalls into the mix, and you start to get the idea.

                                In any case, here's what the code would look like to download a file using SocketTools:

                                #COMPILE EXE
                                #DIM ALL
                                #INCLUDE "WIN32API.INC"
                                #INCLUDE "CSTOOLS5.INC"
                                FUNCTION PBMAIN () AS LONG
                                    LOCAL szLocalFile AS ASCIIZ * %MAX_PATH
                                    LOCAL szRemoteFile AS ASCIIZ * %MAX_PATH
                                    LOCAL ftpStatus AS FTPTRANSFERSTATUS
                                    LOCAL bResult AS INTEGER
                                    szLocalFile = "c:\temp\Filename.ext"
                                    szRemoteFile = ""
                                    FtpInitialize($CSTOOLS5_LICENSE_KEY, 0)
                                    bResult = FtpDownloadFile(szLocalFile, _        ' Local file path/name
                                                              szRemoteFile, _       ' Remote FTP URL
                                                              20, _                 ' Timeout in seconds
                                                              %FTP_OPTION_PASSIVE, _' Options
                                                              ftpStatus, _          ' Transfer status
                                                              %NULL, 0)
                                    IF ISTRUE(bResult) THEN
                                        LOCAL strFileSize AS STRING
                                        LOCAL strSeconds AS STRING
                                        strFileSize = LTRIM$(STR$(ftpStatus.dwBytesCopied))
                                        strSeconds = LTRIM$(STR$(ftpStatus.dwTimeElapsed))
                                        MSGBOX "File downloaded successfully (" + _
                                               strFileSize + " bytes in " + strSeconds + " seconds)"
                                        LOCAL dwError AS DWORD
                                        LOCAL szError AS ASCIIZ * 256
                                        dwError = FtpGetLastError()
                                        FtpGetErrorString(dwError, szError, 256)
                                        MSGBOX "Error " + HEX$(dwError) + ": " + szError
                                    END IF
                                END FUNCTION
                                Pretty straight-forward stuff. There's an FtpUploadFile function as well, so if all you needed to do was simple uploads and downloads, those two functions would cover it. Of course, you can also do more complicated things like list all of the files in a directory on the server, get information about a specific file, send custom site-specific commands and so on.

                                Generally speaking, our API has both high-level functions that cover the most common tasks, and lower-level functions for situations where you need customized behavior. For example, some companies actually use FTP as a front-end to a legacy database server, with custom commands used to specify queries, and "download" operations to retrieve the results.
                                Mike Stefanik


                                • #17
                                  I can vouch for the SocketTools library. I have the subscription and it has paid for itself many times over. I've used it to write FTP applications, email applications, and in software to negotiate secure credit card transactions.

                                  I had a learning curve to get around but Mike provided great support.

                                  It's good stuff.


                                  • #18
                                    As long as we're giving accolades

                                    I too have SocketTools, but admittedly I haven't used it all that much. The only reason is because I haven't had a real need for it yet. But looking at the library, I can attest to the fact that it is well written and very comprehensive.

                                    That said, I really can't say enough good things about Marshallsoft's tools. I know they haven't been around these parts in a long time (don't know why, but they do keep the PB code up to date). While FTP can be 'tricky', I've yet to find a server that won't run any of my FTP applications, which are a significant number, using the same commands for all. The $115 US is very well worth every penny to me. I also have their SMTP tools which are invaluable to me as well.

                                    Again, not to take anything away from SocketTools. When I first needed a good FTP library, SocketTools wasn't around I'm sure however, that their library is just as reliable.
                                    Software makes Hardware Happen


                                    • #19
                                      Of course you can but frankly, i don't really need ftp at this time.
                                      The tutorial would be fun to read but should be based on ordinary API examples.

                                      I was just curious.

                                      I seen a .NET example, something almost with equal value for me (since .NET is about to be present on all computers.. soon though)

                                      Maybe i write me a usefull example with this ever..


                                      • #20
                                        Out of curiousity, I took a look at the project you linked to on While free code is nice, and great for learning, I'd never recommend using this (or something like it) for production code unless you're only interested in doing one-off file transfers. Why?

                                        FTPclient is designed to operate in a stateless mode, in a similar way to how a web request would work. It does not hold open a connection but instead will connect, perform the requested action, and disconnect for each request.
                                        That is a big ouch right there, and for multiple file transfers imposes an enormous amount of overhead to the application. Not to mention that it could run afoul of security measures on some systems which would see connections like this as a potential denial-of-service attack or probe (and will probably start throttling back connection requests to the application).

                                        Edit: One other thought while we're on the subject. While using COM Interop to access .NET classes is certainly possible, I doubt that most PowerBASIC developers would really see that as an attractive option. With each method that's invoked, you're looking at somewhere in the neighborhood of an extra 50 or so instructions (quite possibly more, depending on the marshalling involved). Undoubtably there is convenience there, but you pay a significant performance penalty for it. If one really wants to do .NET programming, then use a .NET language. Right tool for the right job, and all that.
                                        Last edited by Mike Stefanik; 24 Feb 2008, 02:16 PM.
                                        Mike Stefanik