开发者

Getting the remote address from a <script> tag

开发者 https://www.devze.com 2023-01-11 06:07 出处:网络
I have a page with a <script> tag in it pointing to a PHP script that generates JavaScript. In this PHP script, I verify the remote IP to see if this IP has the right to use the script.

I have a page with a <script> tag in it pointing to a PHP script that generates JavaScript.

In this PHP script, I verify the remote IP to see if this IP has the right to use the script.

My question is: how do I get the IP from the machine that embeds the <script> tag?

In $_SERVER, I have REMOTE_ADDR which is my IP address (the client) and SERVER_ADDR which is the IP of the machine that runs the PHP script that generates JavaScript (the server).

But in between those, there's the page that embeds the <script> tag and I need its serve开发者_C百科r IP address.

Any ideas about how I'm gonna find it?

Thanks


Pictures:

client browser -- local gateway -- internet -- html page server
client browser -- local gateway -- internet -- Javascript server

Assuming: You control the Javascript server but not the html page server

Answer: Your javascript (JS) program could send your JS Server the value of window.location. That will tell your JS Server where the page was loaded from (html page server).

Since your JS can't do an Ajax call (different domains), it will have to load something (a script or a web bug img) from your js server and include the window.location and session info it received in the original js file as url parameters.

Notes:

  • if the html page server is behind a firewall you won't be able to contact it directly.
  • if the browser is behind a gateway, then you don't really have the browser address in REMOTE_ADDR, you have the gateway's address


But in between those, there's the page that embeds the tag and I need its server IP address.

You can't reliably get that information. You might find the URL of the requesting page in $_SERVER['HTTP_REFERER'], but not all clients will reliably send that information. Further, referrers can be faked easily.

What are you trying to do? What is your goal in controlling where the script is embedded?

0

精彩评论

暂无评论...
验证码 换一张
取 消