F
F
Flysink2018-10-09 12:48:56
C++ / C#
Flysink, 2018-10-09 12:48:56

How to resend cookies correctly?

Hello!
Unable to save cookies and reuse in another request, minimal example:
There is one button in the widget, on which the print_inf() function is called
File widget.h

void print_inf() {
CURL *curl;
struct curl_slist *cookies;

widget.cpp file
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL); 
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
  curl_easy_setopt(curl, CURLOPT_COOKIELIST, &cookies);
  res = curl_easy_perform(curl);
if(res != CURLE_OK) {
    qDebug() << "bad connect\n";
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
}
else {
    res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
    struct curl_slist *nd;
    nd = cookies;
    qDebug() << "parse cookies";
    while(nd)
    {
       const auto &p = nd->data;
       qDebug() << p;
       const auto &next = nd->next;
       nd = next;
     }
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}

On the server side I set cookies
setcookie("nameCookie", 100, time() + 3600 * 60,'/', '192.168.0.55');
setcookie("nameCookie2", 100, time() + 3600 * 60,'/', '192.168.0.55');
If you make a redirect - cookies are set correctly and it will be visible in the headers [cookies] => nameCookie=value; nameCookie2=value\n
But if you run the print_inf() function again
and check for the existence of cookies on the server
if(isset($_COOKIE["nameCookie"],$_COOKIE["nameCookie2"])) echo 'yes' else 'no';

No cookies are sent and the header is empty:
Array\n(\n    [Host] => 192.168.0.55\n    [User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)\n    [Accept] => */*\n    [test] => 500\n    [test2] => 700\n    [Content-Length] => 25\n    [Content-Type] => application/x-www-form-urlencoded\n)

I make a second request when cookies have already been set, I check the structure (struct curl_slist *cookies), and I see that everything is in order, the cookie data is recorded, but why is it not transmitted?
192.168.0.55	FALSE	/	FALSE	1539293470	nameCookie	100
192.168.0.55	FALSE	/	FALSE	1539293470	nameCookie2	100

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question