Creating GET/POST arrays

Oct 12, 2018 at 10:00pm
Hello guys.

PHP is built with C. How do they manage to create/deliver "GET" and "POST" variable-array? E.g. I send a get request and the server receives "fname=John&lname=Mansson". How do I take this string and generate e.g. GET ['fname'] and GET['lname']. The program is already compiled and running and inputnames can always vary. I dont intend to build for only one scenario


Best regards.
Oct 13, 2018 at 12:13am
Are you asking how to build the HTTP request, or the response? PHP builds responses, not requests. Requests (e.g. GET, POST, PUT) are built by HTTP clients (a.k.a. web browsers).

The data for the request has to come from somewhere. If the client doesn't know anything, then it can only make the simplest request, which is to GET a URL, which will be inputted by the user somehow. For example, the user might enter on their terminal links http://google.com/ . This will cause a GET to that address and will return an HTML document, which links will display. If the user follows a link, the browser will generate another GET to the address declared by the HTML. If, after a while, the user comes to an input field, such as the text editor to post on this site, they will be able to enter to enter some text, and when they hit "submit", a POST request may be sent, with the body containing the text the user entered (the specifics of the in-site communication protocol will be left up to the designer of the site).
Oct 13, 2018 at 12:59am
Your question is unclear. Are you asking how to parse a string like you've shown into a key/value map?
Oct 13, 2018 at 9:45am
The browser is the client in this case. Let's say you are the client (the browser) and I'm the server (the webpage). You visit my webpage and on that page I have a form of html input fields. You enter the input fields and send this to me (the server). I receive the following:

GET / website.html?Fname=Something&lname=Somethingson
Host: xxxxx
Connection: keep-alive and so on .....

I'm only interested in the GET string after "?".

At the moment, I know that the first variable is "fname" and the other is "lname". So I can easily create a variable in advance called "fname" and "lname" and make "fname = Something" and "lname = Somethingson". No problem so far.

But if I do not know the name of the variables/inputnames that i'm going to receive in advance, how can I create variables with those names AFTER they have been sent to the server?
In PHP, variables are created AFTER the inputfields / getvariables have been sent to the server and then you get access to the variables as key / value.

So in this case I could have accessed them through GET['fname'] and GET['lname'].
Oct 13, 2018 at 10:42am
You're mixing distinct ideas.

When you make a HTTP 1.1 request, you can sent Headers along with the request.

When the server replies, it should reply with Headers of its own, like content-length. It also returns Cookies, Status, and of course, Content.

As I understand it, you want to be able to access the Response Headers with a GET() function as PHP does.

I don't know PHP, but if I would expect a reasonable HTTP Response class to have a GetHeaders() method that returned a collection of key/value pairs, possibly:
 
const std::map<key_type, value_type>& GetHeaders().

That's kinda like what you want, except you'd also have GetCookies() and GetContent().

To do it, you have to parse the response. It's pretty straightforward.
Last edited on Oct 13, 2018 at 10:51am
Oct 13, 2018 at 3:37pm
It's not straightforward to create new variables after you have compiled your program and it is running. That's what's happening with php somehow. If the client sends this for example "google.com?usermail=adam@mymail.com", then you have "usermail" as a variable that you can use to access the value "adam@mymail.com". Exactly like this: " GET['usermail'] " .

Do you feel me? :)
Oct 13, 2018 at 3:46pm
PHP is interpreted not compiled
meaning it can dynamically create variables when for example it receives a GET request
Last edited on Oct 13, 2018 at 3:47pm
Oct 13, 2018 at 4:13pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <map>

int main(){
    std::map<std::string, std::string> map;
    while (true){
        std::string k, v;
        std::cout << "Enter variable name (enter nothing to stop): ";
        std::getline(std::cin, k);
        if (!k.size())
            break;
        std::cout << "Enter value: ";
        std::getline(std::cin, v);
        map[k] = v;
    }
    
    while (true){
        std::string k;
        std::cout << "Enter variable name (enter nothing to stop): ";
        std::getline(std::cin, k);
        if (!k.size())
            break;
        auto it = map.find(k);
        if (it == map.end()){
            std::cout << "undefined\n";
            continue;
        }
        std::cout << it->second << std::endl;
    }
}
Oct 13, 2018 at 6:04pm
Do you feel me?
No.

GET['usermail'] is a runtime dictionary lookup. A C++ container that can do that is a map of string to string as helios has shown above.
Oct 13, 2018 at 10:07pm
Allright. Thank you all. I appreciate it
Topic archived. No new replies allowed.