-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);

} 
6
  • 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 Commented Sep 1, 2021 at 10:01
  • 1
    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.) Commented Sep 1, 2021 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. Commented Sep 1, 2021 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. Commented Sep 1, 2021 at 10:22
  • Just read this - stackoverflow.com/questions/12990207/… Apparently it's not possible. Very strange as everything else in the Apache logs is readily available. Commented Sep 1, 2021 at 10:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.