简体   繁体   中英

how to get a GET/POST variable with a c++ CGI program?

my google-fu has failed me, I'm looking for a basic way to get GET/POST data from an html forum page on my server to use in a c++ CGI program using only basic libraries.

(using an apache server, on ubuntu 22.04.1)

here's the code I've tried

the HTML page:

    <!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>

and here's the c++ code I've tried:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
        printf("Content-type:text/plain\n\n");
        printf("hello World2\n");
    
         // 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("\ncomplete\n");
    
    return 0;
    }

I get the feeling 'getenv' is not the right thing to use here.. it works for things like "SERVER_NAME" though.

any thoughts?

okay, 2 different methods, depending if it's a post or get method:

-GET- html:

 <!doctype html>
 <html>
 <head>
 <title>Website title obv</title>
 </head>
 <body>
 Content goes here yay.
 <h2>Sign Up </h2>
 <form action="cgi-bin/a.cgi" method="get">  
    <input type="text" name="username" value="SampleName">
    <input type="password" name="Password" value="SampleName"> 
 
    <button type="submit" name="Submit">Text</button>
 </form>
 </body>
 </html>

and the c++ (script?) to read the get values:

#include <stdio.h>
#include <iostream>

int main()
{
     printf("Content-type:text/plain\n\n");
     
     char *value = getenv( "QUERY_STRING" );  // this line gets the data
     if ( value != 0 ) {
        printf(value);
     } else {
        printf("Environment variable does not exist.");
     }
     return 0;
}

you have to manually parse the data

-POST-

html: just change 'get' to 'post'

c++:

#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

int main()
{
    printf("Content-type:text/plain\n\n");

    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line << std::endl;
    }
    return 0;
    
return 0;
}

again, you'll have to manually parse the data, but now you have those values to play around with yourself.

have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM