setrawcookie
setrawcookie(string $name, string $value = "", array|int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool
Same Set-Cookie registration as setcookie, minus URL-encoding — the value goes into the header verbatim (fits base64/hashes/values already in a safe charset); values containing spaces/commas/semicolons are invalid at the HTTP layer.
C C23
#include <stdio.h>
#include <string.h>
/* hand-rolled percent-encoding: '=' -> %3D (rawcookie would skip this) */
static void encode(const char *in, char *out) {
int o = 0;
for (int i = 0; in[i]; i++) {
if (in[i] == '=') {
out[o++] = '%'; out[o++] = '3'; out[o++] = 'D';
} else {
out[o++] = in[i];
}
}
out[o] = '\0';
}
int main(void) {
const char *value = "SGVsbG8hPQ=="; /* base64-flavoured value (padding '=') */
char enc[64];
encode(value, enc);
printf("raw form: Set-Cookie: tok=%s; Path=/; HttpOnly\n", value);
printf("setcookie form: Set-Cookie: tok=%s; Path=/; HttpOnly\n", enc);
return 0;
}
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
C++ C++26
#include <iostream>
#include <string>
// hand-rolled percent-encoding: '=' -> %3D (setrawcookie would skip this)
static std::string encode(const std::string &in) {
std::string out;
for (char c : in) {
if (c == '=') out += "%3D";
else out += c;
}
return out;
}
int main() {
const std::string value = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
std::cout << "raw form: Set-Cookie: tok=" << value << "; Path=/; HttpOnly\n";
std::cout << "setcookie form: Set-Cookie: tok=" << encode(value) << "; Path=/; HttpOnly\n";
return 0;
}
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
C# v11.0.100
// Uri.EscapeDataString is the .NET counterpart of setcookie's urlencode;
// setrawcookie corresponds to writing the value verbatim
string value = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
Console.WriteLine("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly");
Console.WriteLine("setcookie form: Set-Cookie: tok=" + System.Uri.EscapeDataString(value) + "; Path=/; HttpOnly");
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
Go v1.27.4
package main
import (
"fmt"
"net/url"
)
func main() {
// url.QueryEscape is the counterpart of setcookie's urlencode;
// setrawcookie corresponds to writing the value verbatim
value := "SGVsbG8hPQ==" // base64-flavoured value (padding '=')
fmt.Println("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly")
fmt.Println("setcookie form: Set-Cookie: tok=" + url.QueryEscape(value) + "; Path=/; HttpOnly")
}
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
Java v27.0.1
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
// URLEncoder.encode is the counterpart of setcookie's urlencode;
// setrawcookie corresponds to writing the value verbatim
String value = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
String encoded = URLEncoder.encode(value, StandardCharsets.UTF_8);
System.out.println("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly");
System.out.println("setcookie form: Set-Cookie: tok=" + encoded + "; Path=/; HttpOnly");
}
}
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
JavaScript v26.13.1
// encodeURIComponent is the counterpart of setcookie's urlencode;
// setrawcookie corresponds to writing the value verbatim
const value = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
console.log("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly");
console.log("setcookie form: Set-Cookie: tok=" + encodeURIComponent(value) + "; Path=/; HttpOnly");
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
PHP v8.6.0
// setrawcookie = setcookie minus URL-encoding: the value goes into the header verbatim.
// Typical use: values that are already header-safe on their own (base64, hashes, tokens).
// setcookie() would urlencode: '=' becomes %3D. setrawcookie() keeps '=' as-is.
$b64 = "SGVsbG8hPQ=="; // base64-flavoured value (ends with padding '=')
$okRaw = setrawcookie("tok", $b64, ["path" => "/", "httponly" => true]);
$okStd = setcookie("tok", $b64, ["path" => "/", "httponly" => true]);
$headers = headers_list(); // CLI SAPI never sends headers -> always empty here
echo "setrawcookie: ", var_export($okRaw, true), "\n";
echo "setcookie: ", var_export($okStd, true), "\n";
echo "headers in CLI: ", count($headers), "\n";
echo "raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly\n";
echo "setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly";
setrawcookie: true setcookie: true headers in CLI: 0 raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
Python v3.15.2
from urllib.parse import quote
# quote() is the counterpart of setcookie's urlencode;
# setrawcookie corresponds to writing the value verbatim
value = "SGVsbG8hPQ==" # base64-flavoured value (padding '=')
print("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly")
print("setcookie form: Set-Cookie: tok=" + quote(value, safe="") + "; Path=/; HttpOnly")
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
Rust v1.99.1
// hand-rolled percent-encoding: '=' -> %3D (std has no encoder; the `percent-encoding` crate in real projects)
fn encode(input: &str) -> String {
input.chars().map(|c| if c == '=' { "%3D".to_string() } else { c.to_string() }).collect()
}
fn main() {
let value = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
println!("raw form: Set-Cookie: tok={}; Path=/; HttpOnly", value);
println!("setcookie form: Set-Cookie: tok={}; Path=/; HttpOnly", encode(value));
}
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
TypeScript v26.13.1
// encodeURIComponent is the counterpart of setcookie's urlencode;
// setrawcookie corresponds to writing the value verbatim
const value: string = "SGVsbG8hPQ=="; // base64-flavoured value (padding '=')
console.log("raw form: Set-Cookie: tok=" + value + "; Path=/; HttpOnly");
console.log("setcookie form: Set-Cookie: tok=" + encodeURIComponent(value) + "; Path=/; HttpOnly");
raw form: Set-Cookie: tok=SGVsbG8hPQ==; Path=/; HttpOnly setcookie form: Set-Cookie: tok=SGVsbG8hPQ%3D%3D; Path=/; HttpOnly
Language differences
C: hand-writing Set-Cookie at the CGI layer, "raw" simply means skipping the encoding step — valid only if you've confirmed every character of the value fits RFC 6265 cookie-octets; otherwise you must percent-encode yourself.
C++: same as C; plain std::string splicing works, real projects use a percent-encoding library.
C#: at the framework layer IResponseCookies.Append(name, value, options) writes the value verbatim too — encoding is the caller's job; Uri.EscapeDataString/Uri.EscapeUriString are the encoding tools.
Go: http.SetCookie's Value goes into the header verbatim (no encoding) — Go's default behavior is already "setrawcookie"; encode with url.QueryEscape yourself and read back with url.QueryUnescape.
Java: Servlet's Cookie.setValue goes verbatim into the header; encode yourself with URLEncoder.encode (mind that it turns spaces into +, unlike JS's %20).
JavaScript: nothing built into Node (express's res.cookie encodes with encodeURIComponent by default; passing encode: false gives setrawcookie semantics); browsers' document.cookie never auto-encodes values.
PHP: setrawcookie matches setcookie parameter-for-parameter except for skipping urlencode (7.3+ options array/samesite included, 8.5 adds partitioned); the precondition is a value of RFC 6265-allowed characters only (letters, digits, !#$%&'*+-.^_|~` and printable chars excluding semicolon/comma/quote/backslash/space) — base64/hex/tokens qualify naturally; the read side never decodes: a setrawcookie value arrives in $_COOKIE verbatim, same as setcookie's (the browser echoes it back and PHP does not decode) — the difference is only in the header bytes. A raw value containing spaces/commas/semicolons is invalid at the HTTP layer (browsers reject or truncate).
Python: at the framework layer Flask's resp.set_cookie(name, value) writes verbatim by default; encoding is the caller's choice (urllib.parse.quote).
Rust: the cookie crate's Cookie::new(name, value) goes verbatim into the header; encode yourself with the percent-encoding crate.
TypeScript: same as JavaScript.