elxis sites
strong security
Elxis Defender, FloodBlocker, Logins Recorder, extended access control and administration login page cloak, are only but a few of the standard security features that Elxis CMS provides you with.
download elxis
elxis 2009.2 electra - 10.41mb
Home arrow Guides arrow Developers guides arrow Protect e-mails from spammers using javascript
english greek

Protect e-mails from spammers using javascript

Wednesday, 30 April 2008

You get tired of getting spam e-mails? Protect your e-mails by hiding them from the spam robots. In this guide we will show you how to implement a simple, but effective, javascript based mechanism on your own Elxis extensions to hide e-mail addresses from web robots in order to prevent them from spamming your mailbox. As you probably know web robots can not execute javascript as it is a client-side language. That means that javascript runs on your browser, not on server. So, whatever is written using javascript can be written by them but it can not be executed. The method we will show you is simply enough and you can use it in your own extensions for Elxis CMS.

In HTML a usual e-mail link looks like this:
<a href="mailto:name@domain.com" title="e-mail">name@domain.com</a>

Spiders can easily get your e-mail address from the above link and bomb you afterwards with thousands of spam e-mails.

Using JavaScript to hide e-mails

Now, we will write exactly the same link as before but using javascript this time.

<script type="text/javascript">
<!--
document.write('<a href="mailto:name@domain.com" title="e-mail">name@domain.com</a>');
//-->
</script>

Spiders can not follow the above link but they can still grab the e-mail address as it is visible in HTML source and spiders can see that the link is an e-mail address. We need to confuse them by writting our javascript splitted and in many lines.

<script type="text/javascript">
<!--
document.write('<a href="ma');
document.write('ilto:');
document.write('name');
document.write('&#64;');
document.write('domain.com" title="e-m');
document.write('ail"');
document.write('>name');
document.write('&#64;');
document.write('domain.com</a>');
//-->
</script>

Now its much better! We also used HTML entities to hide the @ symbol. Spiders can not get a valid e-mail address from the above script while the link is fully usable and valid for normal visitors with javascript enabled web browsers. Here is the final example with a combination of javascript and PHP (e-mail is stored inside a PHP variable). This is what you should final use in your custom extension for Elxis CMS.

<?php 
$email = 'name@domain.com';
$parts = split('@', $email);
?>

<script type="text/javascript">
<!--
document.write('<a href="ma');
document.write('ilto:');
document.write('<?php echo $parts[0]; ?>');
document.write('&#64;');
document.write('<?php echo $parts[1]; ?>" title="e-m');
document.write('ail"');
document.write('><?php echo $parts[0]; ?>');
document.write('&#64;');
document.write('<?php echo $parts[1]; ?></a>');
//-->
</script>

Your e-mail is now protected from the spammers!

Other methods

You can also use images to display the e-mail address. Spiders can not read images but you miss the link functionallity in this case (you can use the Elxis build-in Captcha class to generate images from strings). An other solution is to use javascript again but more complicated (with encryption).

< Previous Next >