简体   繁体   中英

Is it possible to monitor server response codes (http status codes) from PHP, and how would it be done?

The server response codes/http status codes (101, 200, 404, etc.) should be sent in headers in every interaction between user and server. How would one monitor the codes from PHP, so that if an error code were sent, it could be linked to user actions logged by the web application, and responded to appropriately, say by locking out or redirecting a user getting repeated 403 (forbidden) codes by trying to access things on the server they shouldn't be?

Check http_response_code :

*If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.*

http://php.net/manual/en/function.http-response-code.php

Basically nohow, as the headers are sent by the web server before the PHP file is parsed and the "real" output is generated. You might use the method http_response_code() , but consider this: if you want to access a folder your www-data does not have read rights, you will get a 403 Forbidden . It is the webserver's message, the PHP is never even touched (because, for the webserver, it doesn't even exist).

The only way you can catch these events if you specifically use a custom ErrorDocument it your configuration or if the response codes are sent out by the PHP file:

if ( !$something )
    header("HTTP/1.1 403 Forbidden");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM