Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Response.php

function Response::sendHeaders

Sends HTTP headers.

Parameters

positive-int|null $statusCode The status code to use, override the statusCode property if set and not null:

Return value

$this

3 calls to Response::sendHeaders()
Response::send in vendor/symfony/http-foundation/Response.php
Sends HTTP headers and content.
StreamedResponse::sendHeaders in vendor/symfony/http-foundation/StreamedResponse.php
This method only sends the headers once.
StreamedResponse::sendHeaders in vendor/symfony/http-foundation/StreamedResponse.php
This method only sends the headers once.
1 method overrides Response::sendHeaders()
StreamedResponse::sendHeaders in vendor/symfony/http-foundation/StreamedResponse.php
This method only sends the headers once.

File

vendor/symfony/http-foundation/Response.php, line 314

Class

Response
Response represents an HTTP response.

Namespace

Symfony\Component\HttpFoundation

Code

public function sendHeaders(?int $statusCode = null) : static {
    // headers have already been sent by the developer
    if (headers_sent()) {
        return $this;
    }
    $informationalResponse = $statusCode >= 100 && $statusCode < 200;
    if ($informationalResponse && !\function_exists('headers_send')) {
        // skip informational responses if not supported by the SAPI
        return $this;
    }
    // headers
    foreach ($this->headers
        ->allPreserveCaseWithoutCookies() as $name => $values) {
        // As recommended by RFC 8297, PHP automatically copies headers from previous 103 responses, we need to deal with that if headers changed
        $previousValues = $this->sentHeaders[$name] ?? null;
        if ($previousValues === $values) {
            // Header already sent in a previous response, it will be automatically copied in this response by PHP
            continue;
        }
        $replace = 0 === strcasecmp($name, 'Content-Type');
        if (null !== $previousValues && array_diff($previousValues, $values)) {
            header_remove($name);
            $previousValues = null;
        }
        $newValues = null === $previousValues ? $values : array_diff($values, $previousValues);
        foreach ($newValues as $value) {
            header($name . ': ' . $value, $replace, $this->statusCode);
        }
        if ($informationalResponse) {
            $this->sentHeaders[$name] = $values;
        }
    }
    // cookies
    foreach ($this->headers
        ->getCookies() as $cookie) {
        header('Set-Cookie: ' . $cookie, false, $this->statusCode);
    }
    if ($informationalResponse) {
        headers_send($statusCode);
        return $this;
    }
    $statusCode ??= $this->statusCode;
    // status
    header(\sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode);
    return $this;
}
RSS feed
Powered by Drupal