在PHP中,创建Cookie的代码如下:
<?php setcookie("user_id", "12345", time() + (86400 * 30), "/"); // 设置一个有效期为30天的Cookie ?>
在C++中,创建和读取Cookie的代码如下:
#include <iostream> #include <ctime> #include <curl/curl.h> int main() { std::string user_id = "12345"; std::string value = "67890"; std::string url = "http://example.com"; // 目标URL std::string post_data = "user_id=" + user_id + "&value=" + value; CURL *curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L); CURLcode res = curl_easy_perform(curl); if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { curl_easy_cleanup(curl); } } // 读取Cookie std::string response = getenv("HTTP_COOKIE"); // 获取环境变量中的Cookie值 if(response.empty()) { std::cout << "No Cookie found" << std::endl; } else { std::cout << "Cookie Value: " << response << std::endl; } curl_easy_cleanup(curl); return 0; }
还没有评论,来说两句吧...