php 使用 htmlentities() 在源代码中混淆 mailto

php obfuscating mailto in source with htmlentities()

我试图在浏览器中正常运行的页面上显示电子邮件地址,但在代码中进行了混淆,希望至少让一些垃圾邮件机器人忽略它们。

我有这个测试代码:

<?php
$email = "fake@test.com";
$mailto = "mailto:" . $email;
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<p>PHP: <a href="<?php echo htmlentities($mailto); ?>"><?php echo htmlentities($email); ?></a></p>
<p>&nbsp;</p>
<p>MANUAL: <a href="&#109;&#x61;&#105;&#108;&#116;&#x6f;&#58;&#102;&#x61;&#x6b;&#101;&#x40;&#x74;&#101;&#x73;&#x74;&#46;&#x63;&#111;&#x6d;">&#x66;&#97;&#107;&#x65;&#64;&#116;&#x65;&#x73;&#116;&#46;&#99;&#x6f;&#x6d;</a></p>
</body>
</html>

两个链接在页面上看起来和工作都很好,但只有 'manual' 一个被编码。

我从 php.net 那里得到关于 htmlentities 如何工作的相互矛盾的信息。

http://php.net/manual/en/function.htmlentities.php

文档指出 "all characters which have HTML character entity equivalents are translated into these entities." 由于字母表中的所有字母都有等价物,我希望每个字符都被转换。但在那个页面的例子中,它表明基本字母不会被转换。

此外,当我查看该页面上的源代码时,php 代码似乎根本不起作用。我的期望是这两个链接在代码中看起来是一样的。这是 'view source'.

的结果
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<p>PHP: <a href="mailto:fake@test.com">fake@test.com</a></p>
<p>&nbsp;</p>
<p>MANUAL: <a href="&#109;&#x61;&#105;&#108;&#116;&#x6f;&#58;&#102;&#x61;&#x6b;&#101;&#x40;&#x74;&#101;&#x73;&#x74;&#46;&#x63;&#111;&#x6d;">&#x66;&#97;&#107;&#x65;&#64;&#116;&#x65;&#x73;&#116;&#46;&#99;&#x6f;&#x6d;</a></p>
</body>
</html>

看来 htmlentities() 根本没有做任何事情。甚至没有对“@”进行编码。

我应该添加一些标志吗? 有一个更好的方法吗? 如果我成功了,这还能对付机器人吗?还是我在浪费时间?

误会可能来自http://php.net/manual/en/function.htmlentities.php

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

http://php.net/manual/en/function.htmlspecialchars.php

的真正含义

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings.

htmlspecialchars() 编码:&"'<>。检查:

print_r(get_html_translation_table(HTML_SPECIALCHARS));

htmlentities() 编码更多的字符,但只有 个字符 在 HTML 中具有特殊意义。检查:

print_r(get_html_translation_table(HTML_ENTITIES));

你可能会看到这样的东西。我在 link 中对其进行了检查,它按预期工作:

$result = preg_replace_callback('/./', function($m) {
                                           return '&#'.ord($m[0]).';';
                                       },
                                       'mailto:fake@test.com');

这会将字符串中的每个字符替换为 &# 然后是字符的 ASCII 值,然后是 ;