I removed the first "Set-Cookie" in the string as you can see in the code below:
// Function to set a cookie
void setCookie(const char* cookieName, const char* cookieValue, int days) {
String cookieHeader = String(cookieName) + "=" + String(cookieValue);
if (days > 0) {cookieHeader += ";expires=" + String(days * 24 * 60 * 60); // Add expiration if days is greater than 0
}
server.sendHeader("Set-Cookie", cookieHeader); // Send the cookie header to the client
Serial.println("Set-Cookie " + cookieHeader);
}
And this is the print-screen result of the serial monitor:
![]()
but the getCookie returns a null and I cannot see any cookie on the smartphone either:
// Function to get a cookie value by name
String getCookie(const char* cookieName) {
String cookieValue = "";
if (server.hasHeader("Cookie")) { // Check if the "Cookie" header is present in the request
String cookies = server.header("Cookie");
int start = cookies.indexOf(String(cookieName) + "="); // Find the position of the cookie name in the header
if (start != -1) {
start += strlen(cookieName) + 1; // Move the start position to the beginning of the value
int end = cookies.indexOf(';', start); // Find the position of the semicolon or the end of the header
if (end == -1) {
end = cookies.length();
}
cookieValue = cookies.substring(start, end); // Extract the cookie value
Serial.println("CookieValue " + cookieValue);
}
}
return cookieValue;
}