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

时间:2014-08-20 15:21:36

标签: php html url

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

2 个答案:

答案 0 :(得分:1)

删除HTML,标题必须在HTTP正文之前发送:

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

如果这还不行,你需要对参数进行URL编码(不确定):

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

答案 1 :(得分:0)

我现在修改了这个以包含所有正确的编码。我测试了它,它应该按预期工作。

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