php输入发送到URL,格式错误

时间:2016-12-13 12:20:27

标签: php input hyperlink get malformed

我想将输入字段内的文本发送到URL并触发$ GET函数。

<form action="" method="get">
URL:<input type="text" name="url" size="100px" placeholder="URL"/>
<select name="url">
    <option value="<?php filter_var($_GET['url'],FILTER_VALIDATE_URL); ?>">testdomain1.de</option>
    <option value="test2">testdomain2</option>
    <option value="test3">testdomain</option>
</select>
<input type="submit" value="Send"/>
</form>

它有效,但链接格式不正确:

我想要这个:

http://127.0.0.1/title/index.php?url=http://www.tech.de/news/google-will-kein-eigenes-auto-mehr-bauen-10092492.html

但我得到了这个

http://127.0.0.1/title/index.php?url=http%3A%2F%2F127.0.0.1%2Ftitle%2Findex.php%3Furl%3Dhttp%3A%2F%2Fwww.tech.de%2Fnews%2Fgoogle-will-kein-eigenes-auto-mehr-bauen-10092492.html&url=%3Cbr+%2F%3E%0D%0A%3Cb%3ENotice%3C%2Fb%3E%3A++Use+of+undefined+constant+url+-+assumed+%27url%27+in+%3Cb%3EC%3A%5Cxampp%5Chtdocs%5Ctitle%5Cindex.php%3C%2Fb%3E+on+line+%3Cb%3E22%3C%2Fb%3E%3Cbr+%2F%3E%0D%0A
你可以帮助我吗?

好的,我阅读了评论,并改为:

<?php filter_var($_GET['url'],FILTER_VALIDATE_URL); ?>

但它不起作用,没有办法将纯文本粘贴到网址?

1 个答案:

答案 0 :(得分:0)

有一种方法可以将纯文本添加到网址,但它不安全,因为您不能使用filter_varhtmlspecialchars之类的清理。

此外,在提交表单之前未设置$_GET['url'],因此首次发送会导致错误。

此处的选项是在action上添加javascript以设置keyup形式,然后,当您按提交时,您将被重定向到此网址。如果这不是您想要的,请告诉我们您的脚本需要做些什么才能得到正确的答案。