我有一个小静态功能,这样我就可以轻松地在我的本地网站上构建 html 有效 url,它在下面;
public static function url($path = false) {
// Build return url with special html characters escaped
return 'http://127.0.0.1/' . htmlspecialchars($path);
}
我有两个网址,一个在锚点内,另一个在表单动作内,它们在下面;
Root::url('test?category=' . $category . '&index=' . $index) // Href
Root::url('test?category=' . $_GET['category'] . '&index=' . $_GET['index']) // Form
GET === $
,您可以在我的静态函数中看到我用来htmlspecialchars
从我的 url 中转义特殊 html 字符的函数。
锚点返回一个有效链接并按预期工作。然而,表单一返回以下内容,当我点击表单提交时,我在浏览器中的网址如下所示。
http://127.0.0.1/test?category=innate&index=0
为什么是这样?我的网站中断,因为它取决于GET
有效的参数。
感谢您的时间,希望这是有道理的。
编辑
我将函数调用的返回值直接插入到表单操作中,
<form
action="<?= Root::url('test?category=' . $_GET['category'] . '&index=' . $_GET['index']); ?>"
method="post">
编辑
html表单如下;
<form action="http://example.com/test?category=innate&index=0" method="post">
锚html如下
<a href="http://example.com/test?category=innate&index=0">
即使我有 GET 参数,这可能与服务器发送 POST 请求有关吗?
编辑#3
好的,所以它与我的函数或我传入的内容有关,我在表单提交中硬输入了 url,它可以工作,没有问题,这意味着它只能是我的函数返回的内容。
我自己看不到我可能是什么!
回答
提交表单后,我正在重定向到同一页面,header
以对抗表单重新提交。标题的字符串是由 生成的Root::url()
。
这花了我两个小时才弄清楚,但是男孩感觉很好!