Practical Web Data Patterns
Header Labels
Turn a request header value into a simple label.
header-label
Request headers can influence response behavior, but static examples should model them with local scalar values.
Header Labels
header_label.php
Replay: real traced execution (multi-file project)
<?php
$accept = "json";
$contentType = "text/plain";
if ($accept === "json") {
$contentType = "application/json";
} elseif ($accept === "html") {
$contentType = "text/html";
}
echo "accept=" . $accept . "\n";
echo "contentType=" . $contentType . "\n";
<?php
$accept = "html";
$contentType = "text/plain";
if ($accept === "json") {
$contentType = "application/json";
} elseif ($accept === "html") {
$contentType = "text/html";
}
echo "accept=" . $accept . "\n";
echo "contentType=" . $contentType . "\n";
<?php
$accept = "text";
$contentType = "text/plain";
if ($accept === "json") {
$contentType = "application/json";
} elseif ($accept === "html") {
$contentType = "text/html";
}
echo "accept=" . $accept . "\n";
echo "contentType=" . $contentType . "\n";
$accept ← json, $contentType ← text/plain
1<?php2$accept→ json = "json"; //@accept="html", "text"3$contentType→ text/plain = "text/plain";$contentType ← application/json
5if ($acceptjson === "json") {6 $contentType→ application/json = "application/json";7} elseif ($accept === "html") {echo "accept=" . $accept . " ";
11echo "accept=" . $acceptjson . "\n";12echo "contentType=" . $contentTypeapplication/json . "\n";outputaccept=json contentType=application/json
$accept ← html, $contentType ← text/plain
1<?php2$accept→ html = "html";3$contentType→ text/plain = "text/plain";45if ($accept === "json") {6 $contentType = "application/json";7} elseif ($accept === "html") {8 $contentType→ text/html = "text/html";9}1011echo "accept=" . $accepthtml . "\n";12echo "contentType=" . $contentTypetext/html . "\n";outputaccept=html contentType=text/html
$accept ← text, $contentType ← text/plain
1<?php2$accept→ text = "text";3$contentType→ text/plain = "text/plain";45if ($accept === "json") {6 $contentType = "application/json";7} elseif ($accept === "html") {8 $contentType = "text/html";9}1011echo "accept=" . $accepttext . "\n";12echo "contentType=" . $contentTypetext/plain . "\n";outputaccept=text contentType=text/plain