Hello Folks,

I'm trying to use php to hide email addresses from harvesters, and have found the following code -

<?php 
function hide_email($email) {
    $character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
    $key = str_shuffle($character_set);
    $cipher_text = '';
    $id = 'e'.rand(1,999999999);
    for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
    $script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
    $script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
    $script.= 'document.getElementById("'.$id.'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"';
    $script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
    $script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
    return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
?>

<p>Some interesting text goes here, followed by email - </p>


<?php echo hide_email('info@mydomain.com'); ?>

This works well, but I'm having trouble editing the last line so that instead of the address being shown, the page displays "Email Us" or "Contact Us". I have tried the following line -

<a href="mailto:<?php echo hide_email('info@mydomain.com'); ?>">Email Us</a>

But the page then displays the following -

[javascript protected email address]">Email Us

The whole line is a clickable link, and although it brings up the email client, it doesn't fill in the "to" field in the email.

Can anyone see where I'm going wrong with this? 🙂

Hope someone can advise.

    Why not just add a PHP-based contact form to your site, and have your email links point to that? Then there is no need to have the email address on the page at all (obfuscated or not).

      NogDog;11038835 wrote:

      Why not just add a PHP-based contact form to your site, and have your email links point to that? Then there is no need to have the email address on the page at all (obfuscated or not).

      Yes, I've been thinking about it a bit more, and I think that's probably the way to go. 🙂 I've been meaning to switch the site to a CMS (Wordpress) so will now feels like a good time to take the plunge. 🙂

        Hmmmm, I can't see anyway to edit posts on this forum? That being the case, excuse my typos!

          Write a Reply...