$_SERVER['HTTP_REFERER'] 是否可以与多种形式一起使用

$_SERVER['HTTP_REFERER'] is it ok to use it with multiple forms

这就是我在每个表单之后使用的返回 multi-page 表单中的上一页的方法:

$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>GO BACK</a>";

但我不确定它是否能正常工作。有没有更好的返回上一页的方法。我不想使用 header (location:),也许是多次提交,即一次提交表单,另一次提交。但我不确定如何正确实施它。

documentation 中所述,并非所有用户代理都设置 referer:

'HTTP_REFERER'

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

换句话说,$_SERVER['HTTP_REFERER']可能是空的。

我宁愿使用 JavaScript:

<a href="javascript:history.go(-1)">Back</a>

有时可以通过应用程序的逻辑来确定上一页。例如,如果"Step 2"页在"Step 1"页之后,那么我会根据逻辑生成URL:/registration/step1/registration/step2等。这是最靠谱的方法。