如何使用CGICC和C ++设置cookie?

时间:2017-10-16 02:07:00

标签: c++ cookies cgi

我需要使用C ++设置一些cookie,但我似乎无法设置它们,而且我对CGICC很不熟悉所以我不知道我哪里出错了。这是我的登录代码(我知道这是非常不安全的,这是家庭作业,没有任何公共面孔)

void login(cgicc::Cgicc formdata, std::vector<User> &users)
{
    bool success = false;
    std::string userName = getValue("username", formdata);
    std::string password = getValue("password", formdata);
    for(auto iter = users.begin(); iter != users.end(); ++iter)
    {
        if(iter->getUsername() == userName && iter->getPassword() == password)
        {
            success = true;
            std::string id = std::to_string(iter->getID());
            std::cout << "<script> alert(\"Login was successful!\"); </script>" << std::endl;
            std::cout << cgicc::HTTPHTMLHeader().setCookie(cgicc::HTTPCookie("username", userName));
            std::cout << cgicc::HTTPHTMLHeader().setCookie(cgicc::HTTPCookie("userID", id));
        }
    }
    if(!success)
        std::cout << "<script> alert(\"Failed to login. Check your credentials and try again\");</script>"<<std::endl;
}

所有其他代码都经过验证,工作正常,只是cookie设置代码是个问题。

1 个答案:

答案 0 :(得分:0)

以此为例:

std::cout << "Content-Type: text/html\n";
std::cout << "Set-Cookie:count=2;\n";
std::cout << "Set-Cookie:user=tizio;\n";
std::cout << "Set-Cookie:password=profdfosfiotjrejiod;\n\n"

这些行必须是html页面中的第一行