Basic CGI Applications

What is CGI? --------------- Common Gateway Interface Typical applications: 1) forms processing 2) gateway, (Oracle DB) 3) virtual documents 4) server side cickable maps => servers - might require specific directories example: public_html/cgi-bin => or file extensions examples: .pl <= Perl extension .cgi Can use: C/C++ C shell Perl Tcl VB Java file exists on server, html page points to file <html> <a href = "frame.cgi"> frame.cgi</a> <a href = "frame.pl"> frame.pl </a> <br> <!-- #include virtual="frame.pl"--> <br> <!-- #include virtual="frame.cgi"--> </html> Perl Code ---------- #!/usr/local/bin/perl print "content-type: text/html\n\n"; print "<HTML> <BODY> <H1>Hello World</H1>\n"; print "</BODY> </HTML>\n"; file for output must be: chmod a+rwx o+w C++ code: ----------- #include <iostream.h> void main() { cout << "Content-type: text/plain\n\n"; cout << "Hello World\n"; } g++ compiler doesn't work well, instead try CC, (Solaris C++ compiler) - you MUST use .C as extension (Note: CC and .C must be in caps!) HTML file to access a the C++ Program ------------------------------------- please note the space following the <!-- must be removed to get these directives to behave as includes and not simple comments <html> <a href="a.cgi">a.cgi</a> <!-- #include virtual="a.cgi"--> </html> cgiwrap is sometimes useful on some systems <!-- #include virtual="cgi-bin/cgiwrap/engweb/____.pl"--> Perl Script that performs file I/O ---------------------------------- #!/usr/local/bin/perl print "Content-type: text/html\n\n"; #indicate mime content print "<HTML>\n"; print "<BODY>\n"; $file = 'test.dat'; #file is in html page directory open(IN, "<$file") || die "Cannot open $file"; #open file for input @lines = <IN>; #put the lines in an array close(IN); foreach $line (@lines) #process lines one at a time { print "<P> $line </P>\n"; } print "</BODY>\n"; print "</HTML>\n"; $out = "results.dat"; #file is in html page directory open(OUT,">$out") || die "Cannot open $out"; #open file for output print OUT "Some Data Lines\n"; for ($i = 1; $i < 5; $i++) { print OUT "$i\n"; } close(OUT); C++ Version that does file I/O ------------------------------ #include <iostream.h> #include <fstream.h> void main() { ifstream in; ofstream out; char S[30]; int i; in.open("test.dat"); // open input file cout << "Content-type: text/html\n\n"; // indicate mime content cout << "<HTML>\n"; cout << "<BODY>\n"; in >> S; cout << "<H1>"; cout << S; cout << "</H1>\n"; in >> i; while (!in.eof()) { cout << i; cout << "<br>"; cout << "\n"; in >> i; } in.close(); cout << "</BODY>\n"; cout << "</HTML>\n"; out.open("results.dat"); //open output file out << "Some Numbers\n"; for (i = 10; i < 20; i++) { out << i; out << "\n"; } out.close(); }

Server Side Includes:
--------------------

note: the space before the # needs to be removed to make these constructs
      behave as directives and not html comments 

<a href="a.cgi">a.cgi</a> <!-- a.cgi is a compiled c++ prog--> ELF��������������� ���4��" �����4� ��(��������4��4�������������������������������������������������������������L��L�����������L�L��������|������������������������������/usr/lib/ld.so.1�������5���A��������������7�������$���%�������<���-�����������������������3���@���*���?���=�������)���>���+�������:���2���0���������������/���"�����������9����������1���(��� �������������������������,����������6������������������������������������������������������������������������������������������������������������������������������������������������������������������!��������������������������������������&�����������.����������5�����������8�������#�������4�������'���;������������������������������������������������������������������� ����������� (����������� ���������������������������� �����D������ �����H������ �����L������ �����X������ ��������������������������������������`����������������������������������������������������������������������������������������������`���h������ �������� ����"���������"�����'���������0����������5�����"������B����"�����P�L������ ���f�����������|�h���"�������������������������������@�����������������������������"�������������������L"���������"������t���P"������������������������������"���������"�����!����!����)�8���"����3��������?��������K��������Z����������z�`���������X������ ��������������L������ ���H����� ��� ������������������@�� ������"�����P���"���cout�_start�thr_keycreate�_mutex_held�_environ�_end�_ex_register�mutex_trylock�_GLOBAL_OFFSET_TABLE_�__0oNIostream_initctv�mutex_destroy�atexit�exit�_init�___Argv�__0oNIostream_initdtv�thr_exit�_DYNAMIC�thr_setspecific�mutex_lock�thr_getspecific�realloc�_exit�mutex_unlock�_ex_deregister�environ�_thr_main�__cg89_used�__cg92_used�__fnonstd_used�__0fOunsafe_ostreamGoutstrPCcTB�_edata�_PROCEDURE_LINKAGE_TABLE_�__fsr_init_value�_etext�_lib_version�main�__environ_lock�_fini�thr_self�mutex_init�libm.so.1�libC.so.5�libw.so.1�libc.so.1�/applications/lang/devpro_v5n1/SUNWspro/lib:/opt/SUNWspro/lib������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������`���������#�������$�������.�������6�������!�������'�������-��������������0�������� �@D��"c, @'��$+���`������-` � `?-)��#-����/  %�� ��+���`� `?`5����+��c %@�# ���@�A�����D@�A"@�Z�������@�� ���@�A���@�A�������������������P!�� `  �' �'!�� ` #��c ���@��@�Aq���!�� `'!�� `  �' �'!�� ` #��` ���@��@�A[���!�� `'!�� `  �' �'!�� ` #��` ���@��@�AE���!�� `'!�� `  �' �'!�� ` #��`$ ���@��@�A/���!�� `'!�� `  �' �'!�� ` #��`< ���@��@�A���!�� `'!�� `  �' �'!�� ` #��`H ���@��@�A���!�� `'�����������������㿠!�� Q��@�@�����������������㿠!�� Q ��@�@�@���������������������?���������������� ����������������� ����������������� ����������������� ����������������� ����������������� ���������������� �����������������������������������㿠@����@8����`� ```�� *�( �` &`@�@*`�`�`��� 6� �* � 6�# �# � `@#?#?#?&# �@ 6�  �# �` @ &# � � �2�&`�& �&`�&` ������������������@����@�0��������@�`���*   � @�����������������@����@�Ԇ��������@�`���*  �@ �"`�㿠@������?�����������7�����㿠>���@������?����������������������T�X��������������������������������������������������00�����<0�����H0�����T0�����`0�����l0�����x0�����0�����0�������������������������� ��� ���� ������������������ ��A������� ����������o�������X������l���������� (���� ������x��� ��� �������������������������� ����������������������������������������Content-type: text/html ���<HTML> �<BODY> �<H1>Hello World</H1> ���</BODY> ����</HTML> ����������������������������������������������������������������������������������������������� ����������� (����������� ���������������������������� �����D������ �����H������ �����L������ �����X������ ��������������������������������������`������������������������������������������������������������������������������������������������������������������������������� �����������D������� ���#�����������/������������6�����������D������������P���������������Q���������������R������������Z�Q��������h���� �����v�����(������������������T���������X���������\��������������������������������D������� ���������������`���h������ ������������"���������"������������ ��������������"���������"����-�L������ ��C����������Y�h���"����g����������n����������s����@����y����������������������"�����������������L"��������"�����t���P"����������������������������"���������"���������!���� �8���"������������"��������.��������=����������]�`��������d�X������ ��~������������L������ ���H����� ��� ������������������@�� ������"�����P���"���a.out�crti.s�_ex_text0�_ex_range0�_ex_shared0�crt1.s�__crt_scratch�values-Xa.c���hello.C�iostream_init�__0CH_STCON_v�__0DH_STDES_v�mt_stubs.cc�thr_key�thr_data�thr_data_len�crtn.s�_ex_text1�_ex_range1�_ex_shared1�cout�_start�thr_keycreate�_START_�_mutex_held�_environ�_end�_ex_register�mutex_trylock�_GLOBAL_OFFSET_TABLE_�__0oNIostream_initctv�mutex_destroy�atexit�exit�_init�___Argv�_END_�__0oNIostream_initdtv�thr_exit�_DYNAMIC�thr_setspecific�mutex_lock�thr_getspecific�realloc�_exit�mutex_unlock�_ex_deregister�environ�_thr_main�__cg89_used�__cg92_used�__fnonstd_used�__0fOunsafe_ostreamGoutstrPCcTB�_edata�_PROCEDURE_LINKAGE_TABLE_�__fsr_init_value�_etext�_lib_version�main�__environ_lock�_fini�thr_self�mutex_init���������������� <����������B4������������������ <����������H4��������@(#)stdlib.h 1.24 94/03/28 SMI�@(#)feature_tests.h 1.6 93/07/09 SMI�@(#)thread.h 1.36 93/12/26 SMI�@(#)signal.h 1.38 94/02/05 SMI�@(#)unistd.h 1.20 94/02/16 SMI�@(#)siginfo.h 1.34 93/08/03 SMI�@(#)machsig.h 1.9 92/07/14 SMI�@(#)time.h 2.40 94/05/24 SMI�@(#)types.h 1.30 93/11/09 SMI�@(#)machtypes.h 1.8 93/07/09 SMI�@(#)isa_defs.h 1.1 93/07/01 SMI�@(#)select.h 1.10 92/07/14 SMI�@(#)time.h 1.21 94/06/08 SMI�@(#)synch.h 1.26 93/04/13 SMI�@(#)machlock.h 1.12 93/06/11 SMI�@(#)synch.h 1.21 93/04/13 SMI��.interp�.hash�.dynsym�.dynstr�.rela.bss�.rela.plt�.text�.init�.fini�.exception_ranges�.rodata�.got�.plt�.dynamic�.ex_shared�.data�.bss�.symtab�.strtab�.stab.index�.comment�.shstrtab�.stab.indexstr��hello.C�ptf;ptx;ptk;s;V=3.1;R=<<C++ 4.2 (ccfe 1.0) 30 Oct 1996>>�/staff/CIS/bmaxim/public_html/cgi-bin/; /applications/lang/devpro_v5n1/SUNWspro/bin/../SC4.2/bin/CC -xs hello.C��mt_stubs.cc�ptf;ptx;ptk;O;s;V=3.1;R=<<C++ 4.2 (ccfe 1.0) 30 Oct 1996>>�/set/lang/seva2/4.2/lang/cafe/lib/misc/sparc-S2/; /set/lang/seva2/4.2/boot2/bin/sparc-S2/SC4.2/../bin/../SC4.2/bin/CC -noex -I\/..\/..\/..\/include\/sparc-S2 -I..\/..\/..\/include\/common -c -xO3 -Qpath \/set\/lang\/seva2\/4.2\/lang\/cafe\/front\/sparc-S2 -KPIC -D_REENTRANT -DELF -DELF_OBJ -DELF -DELF_OBJ ../src/mt_stubs.cc����������������������������������������������������������������������������� �������������������������������� ����������������������������������A������������������������� �� ��� ������������ ���)������� (�� (���l������ ������ ���3������� �� ��,������������������9������������@������������������?������������@������������������E�������D��D���������������������W�������H��H���������������������_�������L��L��� �����������������d�������X��X����������������� ���i����������������������������r������������ ������������������}�������������������������������������`��`���h�������������������������������`��������0����������������������������������������������������������H�������������� ���������������� ���������������������������������������������������������������������C��������������� <!-- #echo var="DATE_GMT"-->

Wednesday, 25-Nov-2009 12:12:22 GMT

<!-- #echo var="SERVER_NAME"-->

www.engin.umd.umich.edu

<!-- #exec cmd="cal"--> <!-- gives a default calendar for current month in Unix-->

[an error occurred while processing this directive]

<!-- #exec cmd="finger bmaxim"-->

[an error occurred while processing this directive]

<!-- #exec cmd="man unzip"--> <!-- would include all 14 unzip man pages--> <!-- #exec cgi="a.pl"-->

[an error occurred while processing this directive]

<!-- #exec cgi="a.out"--> <!-- where a.out is a compiled c++ program-->

[an error occurred while processing this directive]

<!-- #exec cgi="a.cgi"-->

[an error occurred while processing this directive]

<!-- #config errmsg="if error occurs, contact bmaxim@umich.edu"-->

Working with Forms:
-----------------------

It is hard to find examples of form processinng using C++. This site contains several Perl examples of CGI programming and form processing.