2015年2月8日 星期日

cgi 溝通

存成add.html 放在/var/www/html/ (此時已經安裝apache2了)
請輸入num1 , num2

c code 需要編譯 , 根據你的平台而定 , 我用gcc 在linux 編譯為add.cgi名稱 ,並且放置在/var/www/cgi-bin (此時已經安裝apache2了)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
 char *data;
 long num1,num2;

 printf("Content-type: text/html\n\n");

 printf("<title>加法結果</title>");

 printf("<h3>加法結果</h3 > ");

 data = getenv("QUERY_STRING");

 if(data == NULL)
  printf("<p>無值");
 else if(sscanf(data,"num1=%ld&num2=%ld",&num1,&num2)!=2)
  printf("<p>輸入錯誤");
 else
  printf("<p>和為:%ld。",num1+num2);

 return 0;
} 

有個重點 , 如果在action="/cgi-bin/add.cgi發現有下載的動作 ,這是錯的,因為並沒有經過cgi的處理. 我在fedora ,若是直接點選add.html的話 , 你會發現執行失敗(下載或是File Not Found之類錯誤) 但是如果你使用windows的firefox 打上http://192.168.56.101/add.html 連到fedora的apache2 , 你會發現處理正確. 這是菜鳥的血淚之言阿.

code:
https://drive.google.com/file/d/0B8hm-I2M8BD7cUFmQTNJc3FwQmc/view?usp=sharing

沒有留言:

張貼留言