I prevent direct access to a the following file by returning a 405, unless the user submitted a form, with this code:
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header("HTTP/1.0 405 Method Not Allowed");
exit(); } else {
if(isset($_POST['a'])){
switch ($_POST['a']) {
case "1":
$var = "hey";
break;
case "2":
$var = "now";
break;
default:
$var = "other";
}
}
}
?>
<!doctype html>
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
something
</body>
</html>
Since Chrome and FF display a blank page rather than en error (even though they would display a 404 and not a blank page in case I type a wrong URL, so I'm not sure how this works), I want to create a custom 405 message, So I've added this line to my .htaccess:
ErrorDocument 405 /custom-msg.php
And yet, I still get a blank page rather than the custom message. what's wrong here?
Apache isn't set up to action error documents for PHP set status codes. Similarly to your 404 issue, try:
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header("HTTP/1.0 405 Method Not Allowed");
include 'custom-msg.php';
exit(); }