Sample Web programming (CGI) Code

A C++ program that generates HTML Output

  • Makefile
    The Makefile that compiles all of the examples below.

  • adduserpgm.cpp
    This is an example C++ program that is called from the command line and produces valid HTTP output. A sample call would be: adduserpgm.exe -u Susan -p 1234

    Writing a web program entirely in C++ -

  • addusercgi.cpp
    This is an example C++ program that is called from the html page adduserC.html. The C++ program receives the http string, parses the arguments, then calls a function to do the work. There is no system call necessary.

  • adduserC.html
    This is the web page that calls adduserC.cgi.

  • cgi.cpp
    This is a C++ class that contains better ways to parse http strings.

  • cgi.h
    This is the header for the cgi.cpp class.

    Writing a web program entirely in Perl

  • tinyPL.cgi
    This is an example Perl program that just writes a static web page. No form is necessary to call it.
  • tinyPL.html
    This Web page calls tinyPL.cgi.

  • adduserPL.cgi
    This is an example Perl program that is called from the html page adduserPL.html. The Perl program receives the http string, parses the arguments, then outputs the resulting Web page.
  • adduserPL.html
    This is the web page that calls adduserPL.cgi.

    Writing a web program entirely in PHP

  • Source of tiny.php
    This is an example PHP program that just writes a static web page. No form is necessary to call it. It does not need to reside in your cgi-bin directory. It must reside somewhere in your public_html directory or subdirectories thereof. The filename must end in php.
  • Link to tiny.php to call it Tested 04/2022
  • tinyPHP.html Call it from a web page

  • addstudent.php
    Tested 04/2022
    This is an example PHP program that is called from the html page adduserPHP.html. The PHP program receives the http string, parses the arguments, then outputs the resulting Web page.
  • addstudentPHP.html Tested 04/2022

  • addstudent_singlepage.php Tested 04/2022
    This is an example of a single page php file that generates the html page with a form in it and also processes the data collected from the form.

    Writing a web program entirely in Python

  • Source of tinyPy.cgi Tested 11/2021
    This is an example Python program that just writes a static web page. No form is necessary to call it. It does not need to reside in your cgi-bin directory. It must reside in your cgi-bine directory (subdirectory of public_html). The filename must end in cgi. The group must be the same as your username. e.g., here is the output of ls -l in my cgi-bin directory: -rwxr--r-- 1 sgauch sgauch 771 Oct 31 13:09 tinyPy.cgi

  • addstudentPy.cgi
    This is an example Python program that is called from the html page addstudentPy.html. The Python program receives the http string, parses the arguments, then outputs the resulting Web page. Same rules as above.
  • addstudentPy.html Tested 11/2021

    Creating a web program by calling a C++ program from a Perl script.

  • adduserPLC.cgi
    This is an example Perl program that is called from the html page adduserPLC.html. The perl program receives the http string, parses the arguments, then does a system call to adduserpgm.exe.

  • adduserPLC.html
    This is the web page that calls adduserPLC.cgi.

  • httputils.cpp
    This is a C++ module that contains utilities to generate and parse http strings. I only use the generation utilities these days (see cgi.h).

  • httputils.h
    This is the header for the httputils.cpp module.

    Creating a web program by calling a C++ program from a Perl script. This example reads and writes a temporary file

  • adduserFilePLC.cgi
    This is an example Perl program that is called from the html page adduserPLC.html. The perl program receives the http string, parses the arguments, then does a system call to adduserpgm.exe.

  • adduserpgmFile.cpp
    This is an example C++ program that is called from the command line and produces partial HTML output. Itt assumes that the wrapper program prints the http header and footers. A sample call would be: adduserpgmFile.exe -u Susan -p 1234

  • adduserFilePLC.html
    This is the web page that calls adduserFilePLC.cgi.

    Creating a web program by calling a C++ program from a PHP script.

  • addstudent_staticHTML.php
    This is an example of a single page php file that generates the html page with a form in it that calls a C++ program that generates static output.
  • addstudent_staticHTML.php Tested 11/2021
  • staticHTML.cpp The C++ program called above.

  • addstudent_dynamicHTML.php
    This is an example of a single page php file that generates the html page with a form in it and also passes commandline arguments to a C++ program that generates output based on those commandline arguments.
  • addstudent_dynamicHTML.php Tested 11/2021
  • dynamicHTML.cpp The C++ program called above.

  • addstudent_fileIO.php
    This is an example of a single page php file that generates the html page with a form in it and also passes commandline arguments to a C++ program that does file I/O. The C++ program writes the arguments to a file, reads them back, and then generates output based on the file contents.
  • addstudent_fileIO.php Tested 11/2021
  • fileIO.cpp The C++ program called above.

    Creating a web program by calling a Java program from a Perl script.

  • adduserPLJ.cgi
    This is an example Perl program that is called from the html page adduserPLJ.html. The perl program receives the http string, parses the arguments, then does a system call to java addusercgiJ

  • addusercgiJ.java
    This is the Java program that, given the username and password, echos them back in a HTML page. Do View Source to see the program nicely formatted.

  • adduserPLJ.html
    This is the web page that calls adduserPLJ.cgi.

    Creating a web program by calling a Java program from a Php script.

  • adduserPJ.php
    This is an example Php program that is called from the html page adduserPJ.html. The php program receives the http string, parses the arguments, then does a system call to java adduserPJ.java

  • adduserPJ.java
    This is the Java program that, given the username and password, echos them back in a HTML page. Do View Source to see the program nicely formatted.

  • adduserPJ.html Tested 10/2018
    This is the web page that calls adduserPJ.php.

  • adduserPJ_singlepage.php Tested 16/2020
    This is the web page that calls adduserPJ.php. Replaces aduerPJ.php and adduserPJ.html

    Debugging a script

    To test a script from the command line do the following (assumes that POST is supported by the script): perl adduserPL.cgi
    username=susan&password=1234
    ctrl-D
    You should see the output on the screen. To save to a file, do: perl adduserPL.cgi > out.html
    username=susan&password=1234
    ctrl-D
    Now, edit out.html to remove the top 2 lines of http. Then, out.html should be viewable by a browser. If it isn't you have an html syntax error.