系统:OS X EI Capitan
首先配置apache对CGI的支持
编辑apache配置文件/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf
取消注释
描述什么样的文件视如cgi文件,比如以`.cgi`结尾的文件
AddHandlercgi-script .cgi
返回服务器返回形式
AddType text/html.shtml
AddOutputFilterINCLUDES .shtml
检查cgi_module是否被注释掉了:
LoadModule cgi_module modules/mod_cgi.so
找到并在原有内容上添加以下代码
<Directory>
Options Indexes FollowSymLinks MultiViews ExecCGI
DirectoryIndex index.html index.cgi
AllowOverride None
Order allow,deny
Allow from all
</Directory>
重启 apache
首先编写一个简单的CGI文件
#include<stdio.h>
int main(){
printf("Content-Type:text/html;charset=gbk\r\n\r\n");
printf("<html><head></head><body> fuck </body></html>");
}
此处博主写了一个简单的shell脚本,直接对.c文件进行检查并且用gcc编译
#!/bin/sh
read filename
#获取前缀名称
prefix=${filename%.*}
#获取后缀扩展名称
extension=${filename#*.}
if [ "$extension" = "c" ]
then
result= ` gcc -o $prefix $filename`
if [ $? -eq 0 ]
then
echo "succsss create$prefix.c"
fi
exit 0
fi
exit 0
通过访问cgi,返回html
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
获得用户请求的查询字符串
char * qs = getenv("QUERY_STRING");
char name[250] = {0};
sscanf(qs,"name=%s",name);
if(strcmp(name,"admin")==0){
printf("Location:1.html\r\n\r\n");
}else{
printf("Content-Type:text/html;charset=gbk\r\n\r\n");
printf("<html><head></head><body> <font color='red'>Fuck u man</font> </body></html>");
}
printf("Location:https://www.baidu.com\r\n");
printf("Content-Type:text/html;charset=gbk\r\n\r\n");
printf("<html><head></head><body> <font color='red'>%s</font> </body></html>");
}
调用写好的cgic
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cgic.h"
#include"cgic.c"
int cgiMain(){
char name[256] = {0};
char password[256] = {0};
cgiHeaderContentType("text/html;charset=gbk");
if(cgiFormString("name",name,sizeof(name))!=cgiFormSuccess){
fprintf(cgiOut,"<html><head></head><body><font color='red'>请填写用户名</font></body></html>");
}
if(cgiFormString("password",password,sizeof(password))!=cgiFormSuccess){
fprintf(cgiOut,"<html><head></head><body><font color='red'>请填写密码</font></body></html>");
}
if(strcmp(name,"admin")==0){
cgiHeaderLocation("https://www.baidu.com");
}else{
fprintf(cgiOut,"<html><head></head><body><font color='red'>失败</font></body></html>");
}
来源:oschina
链接:https://my.oschina.net/u/2380832/blog/689191