Programming Tips - php: set HTTP status

Date: 2025mar21 Language: php Q. php: set HTTP status A. Use http_response_code() and header('Status: ...') You need to call those functions before outputting any other HTML/CSS. Here is an example where we tell a robot or user that they are trying to do something that isn't allowed:
http_response_code(405); # Tell the browser or crawler header('Status: 405 Method is forbidden'); # Tell the browser or crawler top(); # Optionally display the top HTML from your website print "<meta name='robots' content='noindex, nofollow'/>\n"; # Request no indexing print "This method is forbidden<br>"; # A message the user sees
More info on the 405 status code https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/405