试图将IP地址输入HTML并使用PHP转换为URL


Trying to enter an IP address into HTML and convert to URL using PHP

也许我疯了,但我已经尝试了几个星期了。我试图运行一个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>

删除HTML,报头必须在HTTP正文之前发送:

<?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;

我现在修改了这一点,以包括所有正确的编码。我测试过了,应该都能正常工作。

    <?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 } ?>