Click here to Skip to main content
15,884,176 members
Articles / Web Development / HTML
Article

Avoiding spam-bots

Rate me:
Please Sign up or sign in to vote.
4.74/5 (35 votes)
31 Aug 20042 min read 132K   753   84   19
Prevent spam-bots from harvesting email addresses in web pages.

Introduction

Spam-bots scan the web and harvest email addresses from web pages, news groups, and other sources. This article shows you a simple technique you can use in web pages to avoid spam-bots. The idea is used in the FotoVision sample I created, but I thought it would be useful to discuss this particular piece outside of the FotoVision sample. The idea is pretty simple; instead of storing the real email address in the HTML, an encoded version of the address is stored and decoded on the client when necessary.

Step 1. Encode the email address

First, the email address needs to be encoded. The encoded string can be pre-calculated or dynamically calculated on the server. The following function uses the BitConverter class to encode the email address derf@example.com to the string 64657266406578616D706C652E636F6D.

C#
// C#
string EncodeEmailAddress(string email)
{
  return BitConverter.ToString(
    ASCIIEncoding.ASCII.GetBytes(email)).Replace("-", "");
}
VB
' VB.NET
Function EncodeEmailAddress(ByVal email As String) As String
  Return BitConverter.ToString( _
    ASCIIEncoding.ASCII.GetBytes(email)).Replace("-", "")
End Function

Step 2. Use the encoded email in the HTML

Instead of using the real email address in the HTML link, use the encoded value. For example:

HTML
<a href="javascript:sendEmail('64657266406578616D706C652E636F6D')">Email Derf</a>

I considered using HTML encoding for the email address, but I think spam-bots would be more likely to process the value and using a custom encoding algorithm is a better solution.

Step 3. Decode the email address on the client

The client-side function sendEmail is called on the client; this function decodes the email address and displays the email application. The sendEmail function contains the following:

JavaScript
// open the client email with the specified address
function sendEmail(encodedEmail)
{
  // do the mailto: link
  location.href = "mailto:" + decodeEmail(encodedEmail);
}

// return the decoded email address
function decodeEmail(encodedEmail)
{
  // holds the decoded email address
  var email = "";

  // go through and decode the email address
  for (i=0; i < encodedEmail.length;)
  {
    // holds each letter (2 digits)
    var letter = "";
    letter = encodedEmail.charAt(i) + encodedEmail.charAt(i+1)

    // build the real email address
    email += String.fromCharCode(parseInt(letter,16));
    i += 2;
  }
  
  return email;
}

That's it, now derf@example.com will not be picked up by spam-bots since the text never appears in the HTML, but the email link still works like expected (the email program is displayed with the correct address when clicked).

Step 4. Optionally, update the status area

You can extend the link by handling the mouseover and mouseout events to display the email address in the status area. The updated HTML link looks like the following:

HTML
<a href="javascript:sendEmail('64657266406578616D706C652E636F6D')" 
  onmouseover="javascript:displayStatus('64657266406578616D706C652E636F6D'); 
  return true;" onmouseout="javascript:clearStatus(); return true;">
  Email Derf</a>

And two functions are added to the client-side script:

JavaScript
// display the email address in the statusbar
function displayStatus(encodedEmail)
{
  window.status = "mailto:" + decodeEmail(encodedEmail);
}

// clear the statusbar message
function clearStatus()
{
  window.status = "";
}

Now, the real email address is displayed in the status area when the mouse is moved over the link.

Sample code and encoding web page

There are two files in the sample code. The file email.js contains the client-side script functions that you can include in your HTML pages. The file test.html is a sample HTML page that uses the email.js file.

The encoded email address can be dynamically calculated on the server, but that's not necessary, you can also pre-calculate the encoded email and use that value in the HTML. I created an encoding web page that encodes an email address that you can paste into your HTML code. If your site contains a lot of email links, it would be easy to create a control that takes in an email address and emits HTML that contains the encoded link.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Ralph Arvesen is a software engineer for Vertigo Software and has worked on desktop, web and Pocket PC applications using .NET and C++. Before Vertigo, he designed hardware and firmware for optical inspection systems and has been developing software for the Microsoft platform since Windows 2.0. He co-authored several books and worked as technical editor on others. Ralph lives in the Texas Hill Country west of Austin; his personal site is located at www.lostsprings.com.

Comments and Discussions

 
GeneralMy vote of 4 Pin
MrsKatz9-Nov-12 13:04
MrsKatz9-Nov-12 13:04 
GeneralInteresting idea Pin
Shane Story28-Jul-09 2:38
Shane Story28-Jul-09 2:38 
QuestionRedirecting the response? Pin
1.21gigawatts15-Dec-05 3:42
1.21gigawatts15-Dec-05 3:42 
GeneralLooks good Pin
Mark Focas9-Sep-04 11:20
Mark Focas9-Sep-04 11:20 
GeneralNice Idea, but not very efficient... Pin
Thomas Schittli31-Aug-04 12:39
Thomas Schittli31-Aug-04 12:39 
GeneralRe: Nice Idea, but not very efficient... Pin
Michael J. Collins1-Sep-04 4:07
professionalMichael J. Collins1-Sep-04 4:07 
GeneralRe: Nice Idea, but not very efficient... Pin
Thomas Schittli1-Sep-04 6:04
Thomas Schittli1-Sep-04 6:04 
GeneralRe: Nice Idea, but not very efficient... Pin
Paul A. Howes2-Sep-04 0:36
Paul A. Howes2-Sep-04 0:36 
GeneralEmail Spoofer .NET Pin
schoolcraftj31-Aug-04 6:27
schoolcraftj31-Aug-04 6:27 
GeneralAssuming Javascript is an issue Pin
Martin Bonner30-Aug-04 22:59
Martin Bonner30-Aug-04 22:59 
GeneralThanks so much! Pin
garyjoh5030-Aug-04 11:26
garyjoh5030-Aug-04 11:26 
GeneralOpens new page or blank page Pin
Anonymous27-Aug-04 5:09
Anonymous27-Aug-04 5:09 
GeneralRe: Opens new page or blank page Pin
Ralph Arvesen27-Aug-04 5:36
Ralph Arvesen27-Aug-04 5:36 
QuestionUse in forms? Pin
Member 101113126-Aug-04 15:49
Member 101113126-Aug-04 15:49 
AnswerRe: Use in forms? Pin
Ralph Arvesen27-Aug-04 9:48
Ralph Arvesen27-Aug-04 9:48 
GeneralRe: Use in forms? Pin
Sander van Driel3-Sep-04 1:14
Sander van Driel3-Sep-04 1:14 
GeneralViewing local test.html file with XP SP2 Pin
Ralph Arvesen26-Aug-04 4:16
Ralph Arvesen26-Aug-04 4:16 
GeneralNice! Pin
Ravi Bhavnani26-Aug-04 0:49
professionalRavi Bhavnani26-Aug-04 0:49 
GeneralCool :) Pin
WillemM25-Aug-04 22:32
WillemM25-Aug-04 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.