PHP setrawcookie() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

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.

PHP
setrawcookie('token', 'a%2Fb', ['path' => '/', 'httponly' => true]);
CallWhat 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
PHP
setrawcookie(string $name, string $value = "", array|int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool

Parameters

ParameterDescription
nameRequired. Cookie name.
valueOptional. Raw cookie value that will not be urlencoded by PHP.
expires_or_optionsOptional. Expiry timestamp, or from PHP 7.3 an options array (expires, path, domain, secure, httponly, samesite).
pathOptional in the classic form. Path where the cookie is available.
domainOptional in the classic form. Domain for the cookie.
secureOptional in the classic form. true to require HTTPS.
httponlyOptional in the classic form. true to block JavaScript access.

Return Value

Return ValueDescription
trueThe Set-Cookie header was queued.
falseThe cookie could not be set.

PHP Examples

Example 1 — Send a Raw Cookie Value

Queue a cookie whose value already contains %2F and confirm success.

PHP
<?php
$ok = setrawcookie('token', 'a%2Fb', [
    'path' => '/',
    'httponly' => true,
]);
var_dump($ok);

How It Works

  1. The value a%2Fb is already encoded for a slash.
  2. setrawcookie() sends it without encoding again.
  3. path and httponly use the options array form.
  4. 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
<?php
setcookie('a', 'x y');
setrawcookie('b', 'x y');

foreach (headers_list() as $header) {
    if (stripos($header, 'Set-Cookie:') === 0) {
        echo $header, PHP_EOL;
    }
}

How It Works

  1. setcookie() urlencodes spaces, often as +.
  2. setrawcookie() keeps the space as provided.
  3. headers_list() reveals both queued headers.
  4. Raw values must stay header-safe on your own.

Example 3 — Options Array with SameSite

Use the PHP 7.3+ array form to set path, HttpOnly, and SameSite together.

PHP
<?php
setrawcookie('rid', 'n8v2', [
    'expires' => time() + 120,
    'path' => '/',
    'httponly' => true,
    'samesite' => 'Strict',
]);

foreach (headers_list() as $header) {
    if (stripos($header, 'Set-Cookie: rid=') === 0) {
        echo 'queued rid cookie', PHP_EOL;
        echo (stripos($header, 'SameSite=Strict') !== false) ? 'samesite ok' : 'missing', PHP_EOL;
    }
}

How It Works

  1. expires is a Unix timestamp two minutes ahead.
  2. SameSite=Strict is only easy with the options array.
  3. We search headers_list() for the rid cookie.
  4. The second line confirms the SameSite attribute was present.

Frequently Asked Questions

It sets a cookie like setcookie(), but leaves the value unencoded.
Only when you must send a value that is already encoded or must not be changed by PHP's urlencoding.
Yes. Both the classic argument list and the PHP 7.3+ options array mirror setcookie().
Yes, with the options array form available since PHP 7.3.
Not by default. Skipping encoding means you are responsible for a valid cookie value. Beginners usually want setcookie().

Next: socket_get_status()

socket_get_status() returns metadata about an open stream, such as whether it is blocked, .

socket_get_status() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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