开发者

How to handle cookies with C++ code?

开发者 https://www.devze.com 2022-12-13 20:42 出处:网络
I am looking for a way to handle sessions through cookies in C++. Can anybody pleas开发者_运维问答e help me with some hints for the solution?libcurl can help you with this. See \"Cookies Without Choco

I am looking for a way to handle sessions through cookies in C++. Can anybody pleas开发者_运维问答e help me with some hints for the solution?


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


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);
    }

    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消