我的google-fu已经失败了我,我正在寻找一个基本的方法来获得get/POST数据从一个html论坛页面在我的服务器上使用的c++ CGI程序只使用基本库。
(使用apache服务器,ubuntu 22.04.1)
这是我试过的代码
HTML页面:
<!doctype html>
<html>
<head>
<title>Our Funky HTML Page</title>
</head>
<body>
Content goes here yay.
<h2>Sign Up </h2>
<form action="cgi-bin/a.cgi" method="post">
<input type="text" name="username" value="SampleName">
<input type="password" name="Password" value="SampleName">
<button type="submit" name="Submit">Text</button>
</form>
</body>
</html>
下面是我试过的c++代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Content-type:text/plainnn");
printf("hello World2n");
// attempt to retrieve value of environment variable
char *value = getenv( "username" ); // Trying to get the username field
if ( value != 0 ) {
printf(value);
} else {
printf("Environment variable does not exist."); // if it failed I get this message
}
printf("ncompleten");
return 0;
}
我得到的感觉'getenv'不是正确的东西在这里使用。它适用于"SERVER_NAME"尽管.
任何想法吗?
<代码>代码>
好的,有两种不同的方法,这取决于它是post还是get方法
- - - - - - -html:
#include <stdio.h>
#include <iostream>
int main()
{
printf("Content-type:text/plainnn");
char *value = getenv( "QUERY_STRING" ); // this line gets the data
if ( value != 0 ) {
printf(value);
} else {
printf("Environment variable does not exist.");
}
return 0;
}
和c++ (script?)来读取get值:
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{
printf("Content-type:text/plainnn");
for (std::string line; std::getline(std::cin, line);) {
std::cout << line << std::endl;
}
return 0;
return 0;
}
你必须手动解析数据
中锋——
html:把'get'改成'post'
c++:
PP_6同样,您必须手动解析数据,但是现在您可以自己处理这些值了。
玩得开心!