CGI脚本是用下列三种方法使用的:
perl cgi 的html 输出
perl-cgi脚本产生某种Web服务器能理解的输出结果
执行CGI,把输出显示在页面上,至少要有一句print "Content-type:text/html\n\n";
否则页面会显示[an error occurred while processing this directive]
content-type实际是限制此句后面的所有输出,你输出的数据应该符合你所规定的content-type
;
Content-Type
HTML
text/html
输出安置应该是在HTML.
Text
text/plain
GIF
image/gif
JPEG
image/jpeg
输出应该是在一个二进制的GIF文件
MPEG
video/mpeg
在print "Content-type: text/html\n\n";之前,print别的信息,浏览器上无显示
print "\n"和print "<br>"都能在HTTP上输出换行?
print "<br>"肯定能输出换行,
只有当cgi脚本是纯perl语句时,\n能在浏览器上输出回车
下面的\n能输出换行
下面的\n,由于被夹在<body> </body>间,无法显示换行
\n被夹在html语句中间时,浏览器是以html语言解释它,则不认识\n
必须改为print "xxx <br>";
可以不输出文档,而是只通知用户浏览器哪儿可以得到这个新文档
Content-type: text/html
Location: /dir1/dir2/myfile.html
采用shell script来编写CGI----- 关键是用echo输出html格式语句
===============================SSI=============================================
什么是SSI(Server Side Includes)?
SSI是HTML页面中的指令,可以把
一小段CGI程序插入到页面中 (而不是CGI程序模拟整个页面)
SSI最典型的特征是index.shtml
SSI最常用的几个地方:
页面计数器
页面当前时间
enable apache SSI (以下两条都不用配,都是缺省就有)
vi httpd.conf
SSI指令的html语法:其格式很象HTML的注释,因此如果没有正确配置SSI,它会被浏览器忽略
执行一个SSI CGI,把输出显示在页面上
两种方法:
<!--#exec功能是极度危险的,因为它会执行任何包含在exec标记中的命令
如果有可以修改网页内容的语句,比如“留言本”,那么一定要关闭这个功能
SSI设置变量
定义
<!--#set var="
modified
" value="$LAST_MODIFIED" -->
应用
<!--#set var="date" value="
$modified
" -->
作为一个表单的ACTION | |
链接link | <A HREF="http://192.168.1.12/cgi-bin/printenv"> Display the Date </A> |
SSI | 将CGI代码嵌在HTML中,称为SHTML |
perl-cgi脚本产生某种Web服务器能理解的输出结果
#!/usr/bin/perl print "Content-type:text/html\n\n"; print "<html><head><title>cgi test </title></head>"; print "<body bgcolor='yellow'>"; print "</body></html>"; |
否则页面会显示[an error occurred while processing this directive]
Content-Type
HTML
Text
GIF
JPEG
MPEG
#!/usr/bin/perl -w 。。。 print "$fileName"; print "Content-type: text/html\n\n"; |
浏览器什么都不显示 |
#!/usr/bin/perl -w print "Content-type: text/html\n\n"; print "$fileName"; |
浏览器显示文件名 |
print "<br>"肯定能输出换行,
只有当cgi脚本是纯perl语句时,\n能在浏览器上输出回车
下面的\n能输出换行
print "Content-type: text/html\n\n"; |
print "<html><head><title>cgi test </title></head>"; print "<body bgcolor='yellow'>"; print "xxx\n"; print "</body></html>"; |
必须改为print "xxx <br>";
Content-type: text/html
Location: /dir1/dir2/myfile.html
[root@mac-home cgi-bin]# vi hello.sh #!/bin/sh echo "Content-type: text/html" echo echo "How are you!" |
普通的unix下执行 | http://192.168.1.12/cgi-bin/hello.sh |
# ./hello.sh Content-type: text/html How are you! | |
===============================SSI=============================================
|
vi httpd.conf
<Directory "/var/www/html"> </Directory> |
AddType text/html .shtml AddOutputFilter INCLUDES .shtml |
<!--#element attribute=value attribute=value ...--> |
两种方法:
<!--#exec cgi="/cgi-bin/counter.pl" --> |
<!--#include virtual="/cgi-bin/counter.pl" --> |
如果有可以修改网页内容的语句,比如“留言本”,那么一定要关闭这个功能
vi httpd.conf <Directory "/var/www/html"> Options Indexes FollowSymLinks IncludesNOEXEC </Directory> |
定义
应用