首页 > 解决方案 > 为什么 HTML 输入元素 ID 属性值在我发送电子邮件的表单中作为前缀?

问题描述

我正在使用 PHP 编写要通过电子邮件发送的 HTML 表单。例如,我有这样的代码:

$userOutput .= '<input type="hidden" id="seenUser" name="seenUser" value="'.$lastUsername.'">';
$userOutput .= '<input type="hidden" id="seenToken" name="seenToken" value="'.$userToken.'">';

当该电子邮件到达我的收件箱时,如果我使用 Firefox 的代码检查器,它会将元素显示为:

<input type="hidden" id="x_683480324seenUser" name="seenUser" value="someUsername">
<input type="hidden" id="x_683480324seenToken" name="seenToken" value="6ed3738a6e20f56f83ec83d9ef77a169">

为什么 id 属性值有前缀x_683480324

在打算接收表单提交的页面上,我正在使用:

if (isset($_POST['seenUser'])&&isset($_POST['seenToken'])) {

...由于不可预测的前缀,这显然是失败的。

长版

这是我的网络邮件中出现的表格。显然onSubmit,我的网络邮件提供商已经添加了。form id得出对值的更改以及我的问题中的值的更改似乎是合理的-input id值 - 可能也被网络邮件提供商修改。

<form id="x_-381141859birdsSeen" action="https://www.MYDOMAIN.com.au/MYPAGE.php" method="post" onsubmit="return window.confirm(&quot;You are about to send information to an external page. Do you want to continue?&quot;);" target="_blank">
    <h4>Pacific Baza <input type="checkbox" name="pacbaz1" value="pacbaz1"></h4>
    2019-10-23 - <a href="https://www.OBFUSCATED.org/" target="_blank">AU-NSW-KRG</a>
    <br>
    <h4>Whiskered Tern <input type="checkbox" name="whiter2" value="whiter2"></h4>
    2019-10-22 - <a href="https://www.OBFUSCATED.org/" target="_blank">AU-NSW-NEW</a>
    <br>
    <input type="hidden" id="x_-381141859seenUser" name="seenUser" value="OBFUSCATED">
    <input type="hidden" id="x_-381141859seenToken" name="seenToken" value="8c1865d190e5a11d2cfe548224bf8d56">
    <input type="submit" value="Seen them!">
</form>

在接受讲座之前,我了解并非所有邮件提供商都支持在电子邮件中使用表格。尽管如此,当我修改我对 $_POST 参数的检查(与上面问题中给出的代码相比)时,此表单确实有效,如下所示:

foreach($_POST as $postName=>$postValue) {

    echo 'PostVar: '.$postName.'<br />';

    if (substr($postName, -8)==='seenUser'&&strlen($postName)>8) { // The POST variable name is longer than 8 chars and the last 8 chars are 'seenUser'
        $postedSeenUser = $postValue; // assign the convoluted variable's value to our nice simple name
    }

    if (substr($postName, -9)==='seenToken'&&strlen($postName)>9) { // The POST variable name is longer than 9 chars and the last 9 chars are 'seenToken'
        $postedSeenToken = $postValue; // assign the convoluted variable's value to our nice simple name
    }

    if ($postName==='seenUser') $postedSeenUser = $postValue;   // Just in case the variable arrives with nice clean name as desired
    if ($postName==='seenToken') $postedSeenToken = $postValue; // Just in case the variable arrives with nice clean name as desired

} // And now our variables should contain nice neat seenUser and seenToken values if they existed in some convoluted manner.

if (isset($postedSeenUser)&&isset($postedSeenToken)) {

虽然可能是最后两个“抓住简单案例”的陈述导致上述内容起作用,但问题仍然是为什么以下内容首先不应该起作用:

if (isset($_POST['seenUser'])&&isset($_POST['seenToken'])) {

标签: phphtmlformshtml-email

解决方案


为什么 id 属性值有前缀x_683480324

通过查看form元素的完整代码,可以清楚地看到表单中还有其他添加内容。一个,特别是(该onSubmit属性)建议是 Web 邮件客户端进行了该特定更改,因此可能其他人也进行了更改。

由于不可预测的前缀,这显然是失败的。

一位评论员指出,代码不可能由于属性而失败,id因为isset对属性进行了操作,而这些name属性仍未修改。

目前还没有解决代码失败的原因。根据问题中的示例,处理这两种情况的解决方法 -isset处理name值和/或id值 - 都有效。


推荐阅读