Home PHP Network setrawcookie() PHP setrawcookie() Function Definition In PHP, setrawcookie() queues a Set-Cookie header just like setcookie(), except the cookie value is not urlencoded by PHP.
Use it when the value must be sent exactly as you built it. The expires, path, domain, secure, httponly arguments and the PHP 7.3+ options array work the same way as setcookie(). Most beginners should prefer setcookie() unless they have a specific raw-value need.
Syntax Call setrawcookie() with the values you need. Start with this line.
setrawcookie('token', 'a%2Fb', ['path' => '/', 'httponly' => true]); Call What it does setrawcookie('token', 'a%2Fb')Sends the value exactly, without a second round of encoding. setrawcookie('x', 'raw+value', time() + 600, '/')Classic form with expiry and path. setrawcookie('x', 'raw+value', ['expires' => time() + 600, 'path' => '/', 'samesite' => 'Strict'])Options array form including SameSite.
The value you pass must already be safe for a cookie header. Invalid characters can produce a broken Set-Cookie line. Call it before any output, the same as setcookie().
Library declarations setrawcookie(string $name, string $value = "", array|int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool Parameters Parameter Description name Required. Cookie name. value Optional. Raw cookie value that will not be urlencoded by PHP. expires_or_options Optional. Expiry timestamp, or from PHP 7.3 an options array (expires, path, domain, secure, httponly, samesite). path Optional in the classic form. Path where the cookie is available. domain Optional in the classic form. Domain for the cookie. secure Optional in the classic form. true to require HTTPS. httponly Optional in the classic form. true to block JavaScript access.
Return Value Return Value Description true The Set-Cookie header was queued. false The cookie could not be set.
PHP Examples 1. Send a Raw Cookie Value 2. Compare with setcookie() Encoding 3. Options Array with SameSite Example 1 — Send a Raw Cookie Value Queue a cookie whose value already contains %2F and confirm success.
<?php
$ok = setrawcookie('token', 'a%2Fb', [
'path' => '/',
'httponly' => true,
]);
var_dump($ok); How It Works The value a%2Fb is already encoded for a slash. setrawcookie() sends it without encoding again. path and httponly use the options array form. true means the header was accepted by PHP. Example 2 — Compare with setcookie() Encoding Show how the same source characters become different Set-Cookie values with the two functions.
<?php
setcookie('a', 'x y');
setrawcookie('b', 'x y');
foreach (headers_list() as $header) {
if (stripos($header, 'Set-Cookie:') === 0) {
echo $header, PHP_EOL;
}
} Set-Cookie: a=x+y
Set-Cookie: b=x y How It Works setcookie() urlencodes spaces, often as +. setrawcookie() keeps the space as provided. headers_list() reveals both queued headers. Raw values must stay header-safe on your own. Frequently Asked Questions What does setrawcookie() do? It sets a cookie like setcookie(), but leaves the value unencoded.
When should I use setrawcookie() instead of setcookie()? Only when you must send a value that is already encoded or must not be changed by PHP's urlencoding.
Do expires, path, domain, secure, and httponly work the same? Yes. Both the classic argument list and the PHP 7.3+ options array mirror setcookie().
Can setrawcookie() set SameSite? Yes, with the options array form available since PHP 7.3.
Is setrawcookie() safer? Not by default. Skipping encoding means you are responsible for a valid cookie value. Beginners usually want setcookie().
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion