-1

Can someone help me please. I can get get the host, referer, requested URI and the agent from visitors to my website but I cannot figure out how to get what response code was returned to the user.

$host = $_SERVER['REMOTE_HOST'];
$referer = $_SERVER['HTTP_REFERER'];
$requested = $_SERVER['REQUEST_URI'];
$agent = $_SERVER['HTTP_USER_AGENT'];

It must be simple but I can't seem to work it out.

FULL CODE:

$varToday = date("d-m-Y");
$filelocation = $_SERVER['DOCUMENT_ROOT']."/logs/";

$ip = getRealIpAddr();
$host = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr($_SERVER['REMOTE_ADDR']);
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
$referer = htmlspecialchars($referer, ENT_QUOTES, 'UTF-8');

$requested = $_SERVER['REQUEST_URI'];
$agent = $_SERVER['HTTP_USER_AGENT'];

$httpCode = http_response_code();

if ($httpCode!=200) {
 $filename = $filelocation."4ECerrors".$varToday.".txt";

 $content = $varDate." - ".$host." - ".$ip." - ".$requested." - ".$httpCode." - ".$referer." - ".$agent."\r";

 $handle = fopen($filename, 'r');
 if (filesize($filename) > 0) {
   $content .= fread($handle,filesize($filename));
 }
 fclose($handle);
 $handle = fopen($filename, 'w+');
 fwrite($handle, $content,strlen($content));
 fclose($handle);

} 
  • I am interested to know what use you would be making of the status code? By-the-way: The web server returns the status, not php normally – RiggsFolly Sep 01 '21 at 10:01
  • 1
    https://www.php.net/manual/en/function.http-response-code.php exists, but I am not sure how helpful that would be. (It will only return explicit values, if those were actively set by your script itself before.) – CBroe Sep 01 '21 at 10:07
  • I have PHP code that emails me a report on a daily basis of certain visitors to my website. But I only want it to add entries to that report if the response code is not 200. – Adrian Hall Sep 01 '21 at 10:09
  • Okay, full code added. As you can see I have tried http_response_code but that doesn't give a value. The response code appears in the apache logs but how can you get that value using PHP. – Adrian Hall Sep 01 '21 at 10:22
  • Just read this - https://stackoverflow.com/questions/12990207/how-to-get-apache-http-response-code-or-first-line-of-server-response-with-php Apparently it's not possible. Very strange as everything else in the Apache logs is readily available. – Adrian Hall Sep 01 '21 at 10:45
  • I know this is just a basic workaround, but you could use CURL. You can create one php file that requests your current page using CURL, and get response status there. Another way would be to get response status in JS and if status is not 200, request some url that will send emails afterwards. – temo Sep 01 '21 at 11:23

1 Answers1

0

You define the status code returned by the server, to the client (browser), via the HTTP response. There is no status code in the HTTP request.

By default PHP will return 200, but you may change it, see https://stackoverflow.com/a/12018482/135494

Clement Herreman
  • 10,274
  • 4
  • 35
  • 57