- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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!