0

How can I get the url of the referer in an ajax request?

A file named main.php has jQuery that sends an ajax call to a file named request.php.

Is it possible to somehow figure out the referer when I'm on request.php? To be precise, I want to print the string "main.php" (dynamically) while running request.php.

sleepless_in_seattle
  • 2,132
  • 4
  • 24
  • 35

2 Answers2

3

Normally the the browser will send the referer page using the header Referer as part of the ajax request so you can read it

So you could do something like

$_SERVER['HTTP_REFERER']

If you don't want to depend on the default Referer header, pass a custom header of your own

$(document).ajaxSend(function (event, jqXHR) {
    jqXHR.setRequestHeader('my-referer', 'some-value');
});
Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

yes on request.php u can get referee like this $_SERVER['HTTP_REFERER']

kki3908050
  • 165
  • 2
  • 9