PHP Network setrawcookie() Function



The PHP Network setrawcookie() function is used to send a cookie to the user's browser. It is similar to the setcookie() function, but with a key difference. Unlike setcookie(), it does not transform the cookie value into a URL-safe format. This function is useful for transmitting cookies containing special characters or data that should not be encoded. It allows you more control over the cookie value, but you must handle encoding manually if necessary.

The function accepts a variety of inputs, including the cookie name, value, expiration time, path, domain, secure flag, and httponly flag. In PHP 7.3.0 and beyond, you can configure these settings using an array of options. If the function is successful, it returns true. If it fails, it returns False. It is important to call this function before providing any output to the browser.

Syntax

Below is the syntax of the PHP Network setrawcookie() function −

bool setrawcookie(
   string $name,
   string $value = ?,
   int $expires_or_options = 0,
   string $path = ?,
   string $domain = ?,
   bool $secure = false,
   bool $httponly = false
)

Alternative signature −

bool setrawcookie(string $name, string $value = ?, array $options = [])

Parameters

Here are the parameters of the setrawcookie() function −

  • $name − (Required) The name of the cookie to be sent to the browser.

  • $value − (Optional) The value of the cookie. By default, it is an empty string. Unlike setcookie(), this value is not URL-encoded.

  • $expires_or_options − (Optional) Either the expiration time as a Unix timestamp or an array of options. The default value is 0, meaning the cookie will expire at the end of the session.

  • $path − (Optional) Specifies the path on the server where the cookie is available. The default is the current directory that the cookie is set in.

  • $domain − (Optional) Specifies the domain where the cookie is available. By default, it is only available to the host that set it.

  • $secure − (Optional) If true, the cookie will be sent only over secure HTTPS connections. The default value is false.

  • $httponly − (Optional) If true, the cookie is accessible only through the HTTP protocol and not by scripting languages like JavaScript. The default value is false.

  • $options − (Optional) An array of options to set the cookie's properties, including expires, path, domain, secure, and httponly. This parameter is only available in PHP 7.3.0 or later.

Return Value

The setrawcookie() function returns TRUE, if the cookie is successfully set. And returns FALSE, if there is a failure in setting the cookie.

PHP Version

First introduced in core PHP 5, the setrawcookie() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Network setrawcookie() function. So this program basically shows setting a simple cookie with a name and value. The cookie expires at the end of the session.

<?php
   // Set a basic cookie with a name and value
   setrawcookie("user", "AnkitMishra");

   // Ensure no output is sent before this point
   echo "Cookie set successfully!";
?>

Output

Here is the outcome of the following code −

Cookie set successfully!

Example 2

In the below PHP code we will use the setrawcookie() function and set cookie with expiration time. So this program shows how to set a cookie that expires after 1 day. It also shows how to calculate the expiration time.

<?php
   // Set a cookie that expires in 1 day 
   $expiration = time() + 86400; 
   setrawcookie("session", "active", $expiration);

   // Ensure no output is sent
   echo "Cookie with expiration set successfully!";
?> 

Output

This will generate the below output −

Cookie with expiration set successfully!

Example 3

Now the below code sets a cookie which is accessible only on a specific path and is sent only over secure HTTPS connections using the setrawcookie() function.

<?php
   // Set a secure cookie with a path restriction
   $expiration = time() + 3600; 
   setrawcookie("preferences", "dark-mode", $expiration, "/settings", "", true);

   // Make sure no output is sent
   echo "Secure cookie with path set successfully!";
?> 

Output

This will create the below output −

Secure cookie with path set successfully!

Example 4

In the following example, we are using the setrawcookie() function to use the options array to configure a cookie with all properties, like expiration, path, domain, secure, and httponly flags.

<?php
   // Set a cookie using the options array
   $options = [
      // Expires in 2 hours
      "expires" => time() + 7200, 
      "path" => "/",
      "domain" => "example.com",
      "secure" => true,
      "httponly" => true,
   ];
   setrawcookie("advanced_cookie", "specialData", $options);

   // Make sure no output is sent
   echo "Advanced cookie set using options array!";
?> 

Output

Following is the output of the above code −

Advanced cookie set using options array!
php_function_reference.htm
Advertisements