[英]How to redirect to another localhost when "Unable to connect Firefox can't establish a connection to the server at ''some ip
我已经为我的android应用程序创建了一个带有php的web服务,在我给出代码的php的标题中
header("Location: http://123.123.123.123/Main/sub.php?details=".$rawdata);
请注意,这是一个封闭的网络ip,这个ip只能从同一个网络访问,所以当我将WIFI网络改为另一个时,我需要将上面的代码更改为
header("Location: http://123.123.123.124/Main/sub.php?details=".$rawdata);
如果不改变它我将收到错误消息"Unable to connect Firefox can't establish a connection to the server at 123.123.123.123"
我想要的是,当我将网络从A切换到B,而不是上面的错误页面时,我希望将页面重定向到第二个代码中指定的位置。 有人请帮忙
我在ubuntu中使用apache2。
您可以通过简单的检查来检查天气主机是否可访问。
$host = 'http://123.123.123.123';
// Number of seconds to wait for a response from remote host
$timeout = 2;
// TCP port to connect to
$port = 80;
// Try and connect
if ($sock = fsockopen($host, $port, $errNo, $errStr, $timeout)) {
// Connected successfully
$up = TRUE;
fclose($sock); // Drop connection immediately for tidiness
} else {
// Connection failed
$up = FALSE;
}
if ($up) {
header("Location: http://123.123.123.123/Main/sub.php?details=".$rawdata);
}
else {
header("Location: http://123.123.123.124/Main/sub.php?details=".$rawdata);
}
通过此检查,您可以轻松地相应地重定向位置。 您可以根据脚本执行时间更改超时。
您可以使用$_SERVER["REMOTE_ADDR"]
来确定客户端的IP地址。 这应该允许您根据需要执行重定向。 ip2long
在这种情况下非常有用,例如:
if ((ip2long($_SERVER['REMOTE_ADDR']) & 0xffffff00) == ip2long('123.123.123.0')) {
header("Location: http://123.123.123.124/Main/sub.php?details=".$rawdata);
} else {
header("Location: http://123.123.123.123/Main/sub.php?details=".$rawdata);
}
ip2long
将IP地址从点分表示转换为long
。 这将允许您执行按位操作,使子网匹配非常简单。
当然,我建议您避免将任何IP范围或地址硬编码到您的代码中。 将它们存储在数据库或其他可以在事情发生变化时轻松维护的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.