Wednesday 04 March 2009

Hiding email addresses with PHPHiding email addresses with PHP

Left QuoteWe all get unnecessary amounts of spam, so here’s a simple way of stopping search engine spiders and spammers from scanning your website pages and finding your email address.

Each character in HTML has an ordinal value, which is it’s ASCII value in the range 0 – 255. The first 31 are reserved for tab, shift, carriage return, end of line etc, but every other value will display a unique ASCII character. For example, the letter "A" has ASCII value 65. Don’t believe me? If you’re on a Windows PC, hold down Alt and press 65 on the keypad. Release the Alt key and "A" will be displayed. Nice.

To display the character in HTML, simply write &#65 and your browser will output A. With this in mind, we can write a simple function that takes your email address, and converts it into a list of ASCII values. The HTML won't look too good, but your browser won't care.



function convertToOrd($str) {
$ordStr="";
for ($i=0; $i<strlen($str); $i++) $ordStr.="&#".ord($str[$i]).";";
return $ordStr;
}

function hideEmail($email_address, $subject) {
$link="mailto:".$email_address;
if ($subject!="") $link.="?Subject=".$subject;
$link=convertToOrd($link);
$email_address=convertToOrd($email_address);
$str="<a href='".$link."' title='Contact Us'>".$email_address."</a>";
return $str;
}

print "If you are having problems, please contact me at ";
print hideEmail('myemail@mydomain.com', 'Website Contact');

?>


The HTML for this is pretty unreadable:

If you are having problems, please contact me at <a href='&#109; &#97; &#105; &#108; &#116; &#111; &#58; &#109; &#121; &#101; &#109; &#97; &#105; &#108; &#64; &#109; &#121; &#100; &#111; &#109; &#97; &#105; &#110; &#46; &#99; &#111; &#109; &#63; &#83; &#117; &#98; &#106; &#101; &#99; &#116; &#61; &#87; &#101; &#98; &#115; &#105; &#116; &#101; &#32; &#67; &#111; &#110; &#116; &#97; &#99; &#116;' title='Contact Us'>&#109; &#121; &#101; &#109; &#97; &#105; &#108; &#64; &#109; &#121; &#100; &#111; &#109; &#97; &#105; &#110; &#46; &#99; &#111; &#109;</a>

The only guaranteed way to hide your email address is to use forms, in which case all your information will be completely hidden, but if you absolutely must display an email address, then at least hide it well ...Right Quote

Next article: Creating Multipart Emails in PHP (15 June 2009)

Next Websites article: Email Address Format Function (24 November 2009)

CommentsComments

Add your comments

Name

Comments

Question

0 + 4 = (this security question stops automated submissions)