views:

274

answers:

2

I am looking for a way to handle sessions through cookies in C++. Can anybody please help me with some hints for the solution?

A: 

libcurl can help you with this. See "Cookies Without Chocolate Chips" here.

Vijay Mathew
A: 

Assuming your C++ code is functioning as a CGI handler, it's merely a matter of reading and writing cookies in the requests and responses.

If your session data is small (less than 32 bytes or so), then you can store it all right in the cookie.

If you need to store more data, or want to share sessions between servers, then you will want to create unique and random IDs to represent your sessions. You should then take that ID and lookup the actual session data (in memory or in a database).

Everything I have written is 1990's CGI 101.

I guess in C++ land, it would look like this:

int main() {

    map<string,string> headers = parseRequestHeaders(cin);

    int64_t sessionId = 0;
    SessionData *session = 0;

    if (getSessionId(headers, &sessionId)) {
        session = getSession(sessionId);
    }
    else {
        session = newSession();
        sessionId = session->id();
        setCookie(sessionId);
    }

    // ...
}
Frank Krueger