JLF JLF - 1 year ago 62
PHP Question

Detect what file AJAX request is coming from in PHP

I'm working on a small application that has many AJAX requests being made to one file in particular.

Once the request is received, I would like the PHP action file to log where the request is coming from and make further decisions. In the PHP action file, I tried using

to determine the location of the AJAX submission, however this returns the location of the PHP action file instead.

Is there any way to use
, or the like to achieve this without needing to manually append another variable to the
object in each form?

Looking for something like:

$origin = $_POST['origin'];
echo $origin; // returns either 'form-submit.js' or '/path/form-page.php'

Answer Source

Without adding another parameter to your Ajax request, the closest thing you can get to what you're looking for is to use $_SERVER['HTTP_REFERER'] in PHP to get the value of the Referer header which says what page the user was coming from.

Of course this value can't be trusted, can't be guaranteed it will be present (some may disable it) or relied upon in general.

You're better off adding another parameter to your request, but this of course can be modified client side too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download