系統:OS X EI Capitancss
首先配置apache對CGI的支持html
編輯apache配置文件/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf
shell
取消註釋apache
描述什麼樣的文件視如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>
重啓 apacheapi
首先編寫一個簡單的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編譯ide
#!/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,返回htmlcode
#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
orm
#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>"); }