Document 7305207

Download Report

Transcript Document 7305207

CGI programming
Peter Verhás
January 2002
What this tutorial is about
• Introduction to CGI programming
• Using ScriptBasic
– Simple to program
– Simple to setup on Linux or any Win
• Prerequisite: understanding web
architecture
How to setup ScriptBasic web test
environment?
• Install ScriptBasic
• Edit the configuration file
– scriba.conf.lsp
– Compile it using cftc.exe
• Start sbhttpd
• Start your browser
(Well, this is not true CGI, but does not matter)
How is CGI Working?
• A new process on the web server
• The CGI process reads stdin and
environment variables
• It writes the stdout
Hello World!
#! /usr/bin/scriba -c
include cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
print """<HTML>
<BODY>
Hello World!
</BODY>
</HTML>
"""
stop
Which Environment Variables?
•
Important environment variables (from the http header)
–
–
–
–
–
–
–
–
–
–
–
–
–
REMOTE_ADDR 127.0.0.1
REQUEST_METHOD GET
REMOTE_HOST localhost
HTTP_USER_AGENT Mozilla/4.0
HTTP_ACCEPT */*
HTTP_CONNECTION Keep-Alive
HTTP_ACCEPT_LANGUAGE hu
SCRIPT_NAME test.bas
SERVER_NAME localhost
SERVER_PORT 80
CONTENT_LENGTH 0
QUERY_STRING q=verh%E1s+%2BCGI
PATH_TRANSLATED /home/httpd/cgi-bin/test.bas
Print Out Environment Variables
#! /usr/bin/scriba -c
include cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
print """<HTML>
<BODY>
<PRE>
"""
i = 0
while IsDefined( Environ(i) )
print i," ",Environ(i),"\n"
i = i+1
wend
print """</PRE>
</BODY>
</HTML>
"""
stop
What is in the QUERY_STRING?
• Anything that the browser sends
• A FORM parameters coded
– parameter=value pairs joined with &
– Space replaced by +
– Special characters %XX hexa encoded
Print Out QUERY_STRING
#! /usr/bin/scriba -c
include cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
print """<HTML>
<BODY>
<PRE>
"""
print Environ("QUERY_STRING")
print """</PRE>
</BODY>
</HTML>
"""
stop
Getting Parameters
#! /usr/bin/scriba -c
include cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
print """<HTML>
<BODY>
<PRE>
"""
print cgi::GetParam("apple")
print """</PRE>
</BODY>
</HTML>
"""
stop
Getting Parameters
#! /usr/bin/scriba -c
include cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
print """<HTML>
<BODY>
<PRE>
<FORM METHOD="POST">
<INPUT TYPE="TEXT" NAME="apple">
</FORM>
"""
print cgi::PostParam("apple")
print """</PRE>
</BODY>
</HTML>
"""
stop
Handling Cookies
File Upload
User Authentication
Thank you for your attention.