防 SPAM 邮件地址
在网页上经常会留下邮件地址及链接,但明文的地址容易被 SPAM 利用,一种简单的方式就是利用 JavaScript 来生成邮件地址。
<?php
/**
* 获取防 SPAM 的邮件链接代码。
*
* @param string $email 邮件地址。
* @param string $text 链接的文字,null 则以邮件地址作为链接文字。
* @param array $attributes 链接标签 <a> 的属性数组。
* @return string
* @author Nio Xiao
*/
function antispam_email($email, $text=null, $attibutes=array())
{
if (!$text) $text = $email;
$attrs = array();
if ($attibutes) {
foreach ($attibutes as $k => $v) $attrs[] = $k.'="'.$v.'"';
}
$html = '<a href="mailto:'.$email.'"'.implode(' ', $attrs).'>'.$text.'</a>';
$chars = array();
for ($i = 0; $i < strlen($html); $i++) {
$chars[] = ord($html[$i]);
}
$str = '
<script type="text/javascript" language="javascript">
document.write(String.fromCharCode('.implode(',', $chars).'));
</script>';
return $str;
}
//测试调用
echo antispam_email('test@somesite.com', 'My Email');
?>
输出的代码:
<script type="text/javascript" language="javascript">
document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,116,101,115,116,64,115,111,109,101,115,105,116,101,46,99,111,109,34,62,77,121,32,69,109,97,105,108,60,47,97,62));
</script>
Comments (3)
