⊗ppPmHpRHP 400 of 447 menu

The Problem With HTTP Response Headers in PHP

According to HTTP rules, the HTTP headers are sent first, and then the body of the HTTP response. Because of this, working with the header function in PHP has its own peculiarities.

The fact is that if there is any output to the screen before calling this function, it will be interpreted as the beginning of the response body. In this case, calling the header function will lead to a PHP warning with the text headers already sent. The headers might even still be sent. The error might not even be displayed (depending on the PHP settings). But most often this will only happen on a local server, and when the site is deployed to the internet, everything will break.

Output to the screen implies any text. For example, like this:

text <?php header('Content-Type: text/html'); ?>

Or an empty line:

<?php header('Content-Type: text/html'); ?>

Or a space:

<?php header('Content-Type: text/html'); ?>

Or a tag:

<div> <?php header('Content-Type: text/html'); ?> </div>

Or output via echo:

<?php echo 'abc'; header('Content-Type: text/html'); ?>

Or output via var_dump:

<?php $str = 'abc'; var_dump($str); header('Content-Type: text/html'); ?>

Even PHP warnings will be considered output to the screen. In the next example, we intentionally access a non-existent variable, which will lead to the output of a warning:

<?php $text += 1; header('Content-Type: text/html'); ?>

Breaking PHP tags also generates output to the screen:

<?php echo 'abc'; ?> <?php header('Content-Type: text/html'); ?>

Intentionally create output to the screen before the header function. Study the text of the resulting error.

Fix the error made in this code:

<!DOCTYPE html> <html> <head> <?php header('Content-Type: text/html'); ?> </head> <body> text <body> <html>
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline