0

Using PHP I would like force a 404 error in the browser.

We are using a homegrown Load Balancer for testing and what is happening is the shared storage sometimes goes offline. The load balancer healthcheck page (example.com/health.php) checks for a 200 response. If 200, page is health, any other status code will mark the node down.

What I would like to do is have php call a file from the shared storage and check if the file exist.

If the shared storage is up and files is the mount example.com/files/isup.htm would generate a 200.

If the page does not exist ie. storage is down and the mount point cannot be reached 404 error.

Problem I am having using the code below:

<?php
$ch = curl_init('example.com/gdkjfh.png');
curl_exec($ch);
if (curl_errno($ch)) {
} else {
header("Location: dkjlflkjxfgkdnbcxfn.html");
}
curl_close($ch);
?>

From question: How can I create an error 404 in PHP?

<?php 
$ch = curl_init('http://example.com/dfdkfh.png');
curl_exec($ch);
if (curl_errno($ch)) {
} else {
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
}
curl_close($ch);
?>

However the load balancer still see a 200 response from example.com/health.php. Also bringing the page up in a browser will generate a 200. Therefore I think what I have to do is generate a 404(or any other status code not 200) within the browser.

enter image description here

How can I force a 404 in the browser using php? I am trying to break the page, just like if you visit ttp://example.com/dfdkfh.png in the network console you will see a 404

No .htaccess no modify the httpd.conf

Donna Delour
  • 117
  • 3
  • @RiggsFolly forgive me, little new to this site, I did see the post you refer to as duplicate, which I tried and did not work, are people not as helpful here as serverfault? – Donna Delour Nov 13 '18 at 20:14
  • I hope we are as helpful. Ok, so please show us what you tried and what is wrong with the result you get, from using one or both of the suggestion provided in the DUP. If necessary I will remove the DUP. – RiggsFolly Nov 14 '18 at 09:11
  • @RiggsFolly I have updated the question – Donna Delour Nov 14 '18 at 16:53
  • 2
    Maybe what you are trying isn't working because `(curl_errno($ch))` is true? – GrumpyCrouton Nov 14 '18 at 16:56
  • As curl errors are all positive integers maybe try `if (curl_errno($ch) > 0) { // we have an error}` or check for specifically relevant curl errors [see the manual](http://php.net/manual/en/function.curl-errno.php) – RiggsFolly Nov 14 '18 at 17:16
  • @RiggsFolly I was able to get the 404 working, the duplicate did help, turns out I was running an older version of php (5.4) when i upgraded to 7.2 it worked fine. Thank You – Donna Delour Nov 16 '18 at 14:48

0 Answers0