Home >Backend Development >PHP Tutorial >Why is $_SERVER[\'HTTP_X_REQUESTED_WITH\'] Missing from the Official PHP Documentation, and Is It Reliable?
Introduction
Developers commonly employ $_SERVER['HTTP_X_REQUESTED_WITH'] to distinguish between AJAX and non-AJAX requests. However, the official PHP documentation fails to list this variable, leading to doubts about its existence.
Question
Why is $_SERVER['HTTP_X_REQUESTED_WITH'] absent from the official PHP documentation, and does it actually exist?
Answer
The Origin of $_SERVER Variables
The variables within $_SERVER originate from the web server, not PHP itself. Consequently, they are not documented in the PHP documentation.
$_SERVER['HTTP_X_REQUESTED_WITH'] Functionality
This variable generally reflects the X-Requested-With header sent by Ajax functions of major frameworks. However, certain frameworks, like Dojo, recently added this header.
Reliability Limitations
$_SERVER['HTTP_X_REQUESTED_WITH'] is not a completely reliable indicator of AJAX requests as not all frameworks consistently send the X-Requested-With header.
Alternative Solution
For absolute certainty, send a pre-defined flag (e.g., a GET variable) with the request and check its presence on the receiving page to determine if it's an AJAX request.
The above is the detailed content of Why is $_SERVER[\'HTTP_X_REQUESTED_WITH\'] Missing from the Official PHP Documentation, and Is It Reliable?. For more information, please follow other related articles on the PHP Chinese website!