简体   繁体   English

如何检查本地服务器是否可以用php访问?

[英]How to check if local server is accessible with php?

My website runs on a public web server A which does not see my local server B located on my intranet. 我的网站在公共Web服务器A上运行,该服务器看不到位于我的Intranet上的本地服务器B。 In my browser, if I am in my office, I can access my intranet in the address field of my browser. 在浏览器中,如果我在办公室,则可以在浏览器的地址字段中访问我的Intranet。 I can also display an image located in my local server B on a page built from my web server A. However if I use file_exists(file on server B) in php running on server A, it does not work as the php script is running on server A which does not see server B. 我还可以在从Web服务器A构建的页面上显示位于本地服务器B中的图像。但是,如果我在服务器A上运行的php中使用file_exists(服务器B上的文件),则该脚本将不起作用,因为php脚本正在运行在看不到服务器B的服务器A上。

I need to detect if the browser can access server B (is in the office or tunnelling via vpn) so I can decide if I display/download the local files on server B or the files on the web server A. How can I do that? 我需要检测浏览器是否可以访问服务器B(是在办公室还是通过vpn进行隧道传输),以便我可以决定是否显示/下载服务器B上的本地文件或Web服务器A上的文件。 ?

I also need that function to see if I can call my asterisk server to place a call on my pabx. 我还需要该函数来查看是否可以调用星号服务器以在pabx上进行呼叫。

If I understand you correct, this http://php.net/manual/en/function.get-headers.php might be helpful. 如果我理解您的观点正确,那么此http://php.net/manual/en/function.get-headers.php可能会有所帮助。 You can "ping" any file on remote server, check returned headers and if returned code is 200 - server is accessble. 您可以“ ping”远程服务器上的任何文件,检查返回的标头,以及返回的代码是否为200-服务器可访问。

When you access the server A, the script can evaluate your address (your office address) and know that it is in a set of addresses that can show images from server B. If you access server A from outside the office, the script can then be set to not display images from B. 当您访问服务器A时,脚本可以评估您的地址(您的办公室地址),并且知道它在一组可以显示来自服务器B的图像的地址中。如果您从办公室外部访问服务器A,则脚本可以设置为不显示来自B的图像。

You find the appropriate variable in 您可以在以下位置找到合适的变量

while(list($key, $val) = each($_SERVER))
{
    $$key = $val;
    printf("%s = %s  \n",$key, $val);
}

Of course the proposed solution to look at HTTP headers above and evaluate if 当然,建议的解决方案可以查看上面的HTTP标头并评估是否

[0] => HTTP/1.1 200 OK [0] => HTTP / 1.1 200 OK

is present or not when trying to reach server B would do the trick as well, but you need to do a failed access to a server you might already know from the IP will fail. 尝试到达服务器B时是否存在,也可以解决该问题,但是您需要对可能已经从IP知道的服务器进行失败的访问。

Admitting, this is a "hardcode" scenario as you need to understand what IP is OK at server A - and that may change. 承认,这是一个“硬编码”方案,因为您需要了解服务器A上的IP是可以的,并且可能会发生变化。 Possibly a javascript solution in the client would be more smooth in this case. 在这种情况下,客户端中的javascript解决方案可能会更加流畅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM