Article

Spamproof Your Site

Page: 1 2

Technique #1: Use JavaScript To Mask Email Addresses

One of the weaknesses that spiders of all kinds suffer from is an inability to process scripts. Adding a small snippet of JavaScript in place of an email address effectively renders the address invisible to spiders, while leaving it accessible to your visitors with all but the most primitive Web browsers.

In the three examples below, simply substitute your username (the first half of your email address, everything before the @ symbol) and your hostname (everything after the @ symbol). To use the scripts, just insert them into your page's HTML wherever you need them to be displayed.

Create A Spam-Proof Mailto Link

This snippet of JavaScript creates a clickable link that launches the visitor's email application, assuming that their system is configured to work with "mailto:" hyperlinks. You can replace the link text with your own message, but see example 2 if you want to display your email address as the link text.

<script language=javascript>  
<!--  
var username = "username";  
var hostname = "yourdomain.com";  
var linktext = "Click Here To Send Me Email";  
document.write("<a href=" + "mail" + "to:" + username +  
"@" + hostname + ">" + linktext + "</a>")  
//-->  
</script>

A Spam-Proof Mailto Link That Shows Your eMail Address

Some visitors won't be able to use a mailto link. This snippet shows your email address in the link so they can copy and paste, or type it by hand:

<script language=javascript>  
 
<!--  
var username = "username";  
var hostname = "yourdomain.com";  
var linktext = username + "@" + hostname;  
document.write("<a href=" + "mail" + "to:" + username +  
"@" + hostname + ">" + linktext + "</a>")  
//-->  
</script>

Display Your Email Address Without A Mailto Link

Here's a snippet that displays your email address without a clickable link:

<script language=javascript>  
<!--  
var username = "username";  
var hostname = "yourdomain.com";  
var linktext = username + "@" + hostname;  
document.write(username + "@" + hostname)  
//-->  
</script>

Technique #2: Use A Contact Form

Sometimes, the sheer volume of legitimate email from real visitors can become a burden. In this case, a simple solution is to remove your email address from your site entirely, and use a contact form. There are dozens of free ASP, Perl, and PHP scripts available online that will allow your users to fill in a form, and send you an email. Most hosting providers now offer this service for free to their customers.

A contact form can enable you to deal with a higher volume of mail, by allowing you to pre-sort different types of message. This is easily accomplished by creating a drop-down menu with different options (e.g. customer service, billing, tech support, etc.) that will populate the subject line of the email message, and/or change the email address to which the form is sent.

As many spambots simply read the entire HTML source of the page in search of anything that looks like an email address, your contact form may not protect you, if you include your email address in the form's HTML (for example, as a hidden field). You can use JavaScript, as shown in the example below, to mask the address, or if you have the skill, you can embed the email address in your form processing script, where nobody can find it.

Masking The Email Address In A Form Field

Instead of simply listing your email address in a form field, use the snippet below to replace the form field that contains your email address.

<script language=javascript>  
<!--  
var username = "username";  
var hostname = "yourdomain.com";  
var linktext = username + "@" + hostname;  
document.write("<input type=hidden name=email  
value=" +username + "@" + hostname" + ">";  
document.write(username + "@" + hostname);  
//-->  
</script>

Advanced Techniques: URL Rewriting

Both the Apache and IIS Web servers have plug-in URL-rewriting modules that can be used to provide additional protection to your site, redirecting queries from known spambots to a blank page, or to another Website. These techniques are beyond the scope of this article, and using them will slow your server down, if only a little.

I hope that this tutorial has given you a clear understanding of how to protect your Website, and your email address, from spammers and spambots.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: