当“无法连接Firefox无法在某个ip上建立到服务器的连接”时,如何重定向到另一个本地主机

时间:2022-10-28 00:15:45

I have created a webservice with php for my android application, in the header of the php I have given the code

我已经为我的android应用程序创建了一个php webservice,在php的头文件中,我已经给出了代码。

header("Location: http://123.123.123.123/Main/sub.php?details=".$rawdata);

Please note that this is a closed network ip, this ip can only be accessed from the same network so when I change the WIFI network to another, I need to change the above code as

请注意,这是一个封闭的网络ip,这个ip只能从同一个网络访问,所以当我把WIFI换到另一个网络时,我需要把上面的代码改为

header("Location: http://123.123.123.124/Main/sub.php?details=".$rawdata);

If dont change it I will get the error message as "Unable to connect Firefox can't establish a connection to the server at 123.123.123.123"

如果不更改,我将会收到错误信息:“无法连接Firefox无法在123.123.123与服务器建立连接”

What I want is that when I switch network from A to B, instead of the above error page I want the page to be redirected to the location specified in the second code. Somebody please help

我想要的是,当我将网络从A切换到B时,我希望页面被重定向到第二个代码中指定的位置,而不是上面的错误页面。有人请帮

I am using apache2 in ubuntu.

我在ubuntu中使用apache2。

2 个解决方案

#1


2  

You can do it by putting simple check for checking weather host is accessible or not.

您可以通过设置简单的检查来检查是否可以访问天气主机。

  $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);
   }

by this check you can easily redirect the location accordingly. you can change the timeout as per your script execution time.

通过此检查,您可以很容易地相应地重定向位置。您可以根据脚本执行时间更改超时。

#2


2  

You can use $_SERVER["REMOTE_ADDR"] to determine IP address of the client. This should let you perform the redirect as needed. ip2long is quite useful in this context, e.g.:

您可以使用$_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 converts the IP address from dotted notation into long. This will let you perform bitwise operations which make subnet matching very straight-forward.

ip2long将IP地址从点符号转换为long。这将让您执行位操作,使子网匹配非常简单。

Of course I'd suggest you avoid hardcoding any IP ranges or addresses into your code. Have them stored in a database or somewhere else where they can be easily maintained when things changes.

当然,我建议您避免在代码中硬编码任何IP范围或地址。将它们存储在数据库或其他地方,以便在发生更改时可以轻松维护它们。

#1


2  

You can do it by putting simple check for checking weather host is accessible or not.

您可以通过设置简单的检查来检查是否可以访问天气主机。

  $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);
   }

by this check you can easily redirect the location accordingly. you can change the timeout as per your script execution time.

通过此检查,您可以很容易地相应地重定向位置。您可以根据脚本执行时间更改超时。

#2


2  

You can use $_SERVER["REMOTE_ADDR"] to determine IP address of the client. This should let you perform the redirect as needed. ip2long is quite useful in this context, e.g.:

您可以使用$_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 converts the IP address from dotted notation into long. This will let you perform bitwise operations which make subnet matching very straight-forward.

ip2long将IP地址从点符号转换为long。这将让您执行位操作,使子网匹配非常简单。

Of course I'd suggest you avoid hardcoding any IP ranges or addresses into your code. Have them stored in a database or somewhere else where they can be easily maintained when things changes.

当然,我建议您避免在代码中硬编码任何IP范围或地址。将它们存储在数据库或其他地方,以便在发生更改时可以轻松维护它们。