【问题标题】:Trying to enter an IP address into HTML and convert to URL using PHP尝试将 IP 地址输入 HTML 并使用 PHP 转换为 URL
【发布时间】:2014-10-14 00:55:56
【问题描述】:

也许我疯了,但我已经尝试了几个星期了。我正在尝试运行一个 PHP 脚本,当它通过 HTML 执行时,它会输入他们通过表单输入的任何 IP 地址,将自动使用地址栏中的 IP 重定向用户,或者打印 URL 以供他们复制并粘贴到他们的地址栏。基本上我希望用户被引导到我网站上的链接,他们在其中输入他们的 IP 地址,当他们这样做时,它会将他们重定向到这个 URL,以便他们可以配置他们的 IP 电话。

http://<Local IP Address>/admin/resync?http://voipcfg.planbcorp.com/initialize.xml

<Local IP Address> 被替换为他们在 HTML 表单中输入的地址。

这可能吗?

我的 HTML 文档如下所示:

<html>
<head>
<title>Phone Provisioning Procedure</title>
</head>
<body>
<form method="POST" action="addIP.php">
<p><strong>Place Your IP Here:</strong><br/>
<input type="text" name="userip"/>
<p><input type="submit" value="Add my IP"/></p>
</form>
</body>
</html>

我的 PHP 脚本看起来像:

<html>
<head>
<title>Phone Provisioning Procedure</title>
</head>
<body>
<h1>Copy this or Select the URL to sync your Phone</h1>
<?php
$userip=$_POST['userip'];
$location = 'http://$userip/admin/resync?http://voipcfg.planbcorp.com/initialize.xml'
header('Location:' .$location);
?>
</body>
</html>

【问题讨论】:

  • URL中的scheme是他们自己的IP地址,怎么会重定向到你的站点呢?
  • 由于同一个IP地址下可以有多个站点,并且IP地址上的哪些站点可以随心所欲地改变,我很确定不可能从IP确定FQDN
  • @NickJ 反向 DNS 通常设置得很好。问题是,你想依赖它吗?可能不会,因为反向 DNS 主机名通常特定于 ISP。
  • 您的每个用户的手机上都运行着网络服务器吗?
  • 还有他们如何找到自己的IP地址,这可能是可能的,但大多数用户不知道如何。

标签: php html url


【解决方案1】:

去掉 HTML,headers 必须在 HTTP body 之前发送:

<?php
$location = 'http://'
          . $_POST['userip'] .
          '/admin/resync?http://voipcfg.planbcorp.com/initialize.xml'
header('Location: ' .$location);
exit;

如果这还不起作用,您需要对参数进行 URLencode(不确定):

<?php
$location = 'http://'
          . $_POST['userip']
          . '/admin/resync?'
          . urlencode('http://voipcfg.planbcorp.com/initialize.xml');
header('Location: ' .$location);
exit;

【讨论】:

  • @RiggsFolly 我们在同一时间编辑我认为。无论如何谢谢:)
【解决方案2】:

我现在对其进行了修改,以包含所有正确的编码。我对其进行了测试,它应该都能按预期工作。

    <?php
    // I would strip it of anything not a number and period since you are possibly outputting it to browser
    //  $userip =   preg_replace("/[^0-9\.]/","",$_GET['userip']);
     $userip        =   preg_replace("/[^0-9\.]/","",$_POST['userip']);


    // Use double quotes
    // Make sure this is a valid address
    $location   =   "http://$userip/admin/resync/?".urlencode('http://voipcfg.planbcorp.com/initialize.xml');
    // Check to see that the file exists in the url you are wanting to redirect to,
    // if xml file is in the location (there are probably better ways to check if a
    // file exists but I generally use file_get_contents for small files), then redirect
    if(file_get_contents($location))
        header('Location:' .$location);
    // If not a real location write out your page with a link
    else { ?><head>
<title>Phone Provisioning Procedure</title>
</head>
<body>
<h1>Copy this or Select the URL to sync your Phone</h1>
<p>URL: <?php echo $location; ?></p>
<a href="<?php echo $location; ?>">Click to go to the address</a>
</body>
</html>
<?php } ?>

【讨论】:

  • 好吧,我明天回到办公室时会尝试测试一下。谢谢你的帮助!如果事情不起作用或不起作用,我会通知您!
  • 关键是你的$location实际上是有效的,否则它永远不会重定向而是显示html页面。
  • 所以当我将脚本添加到我的主网页时,他们输入了他们的 IP 命中提交,您会收到 HTTP 500 内部服务器错误。
  • 我刚刚注意到的另一件事是我的代码有错误。应该是:file_get_contents( 而不是 get_file_contents(。这会导致错误!对不起,我的坏那里!我总是写错!
  • 另外urlencode() 将帮助它在重新加载时不会失败。当我运行代码时,它会检查页面是否存在,但最终会正确打印出来。
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2013-05-09
  • 2011-02-25
相关资源
最近更新 更多