HTTP-antwoordstatus terugstuur in PHP
Jy kan die HTTP-antwoordstatus apart spesifiseer,
sonder om die aanvangslyn te noem.
Dit word gedoen met die funksie
http_response_code:
<?php
http_response_code(404);
?>
Voltooi die kode sodat die ooreenstemmende HTTP-statusse teruggestuur word:
<?php
$arr = ['a', 'b', 'c'];
if (isset($_GET['key'])) {
$key = $_GET['key'];
if (isset($arr[$key])) {
echo $arr[$key];
} else {
// stuur 404 terug
echo 'Not Found';
}
} else {
// stuur 403 terug
echo 'Forbidden';
}
?>