3

I was creating a PHP file that generates a JavaScript code.

In case a website reefers to my code:

<script src="http://myserver.com/myJS.php"></script>

My Question: Is there a way in PHP to find out the WEBSITE's IP that's pointing to my script (Instead of the IP of the visitors of this website) ???

Thanks !

David Katz
  • 33
  • 3

2 Answers2

0

You could try looking at the REFERER http header, which should contain the URL of the site, but there is no guarantee that it will give you the correct value.

// First get the referrer
$referer = $_SERVER['HTTP_REFERER'];

// Parse the referer url
$parts = parse_url($referer);

// Then do a NS lookup to get the IP
$ip = gethostbyname($parts['host']);

Just make sure that your are aware of the fact that the REFERER header can be faked. And the NS lookup is not always fast - or correct.

Can I rely on Referer HTTP header?

https://en.wikipedia.org/wiki/HTTP_referer

Community
  • 1
  • 1
Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
0

What you need is :

$referer = $_SERVER['HTTP_REFERER']

then do:

$parse   = parse_url($referer);
$host    = $parse['host'];

To get IP:

$ip      = gethostbyname($host);

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. SOURCE

Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
Sabari
  • 6,205
  • 1
  • 27
  • 36