vin.ingine.nl
a blog by Vincent van Ingen
5 Aug 2009
 

If you build a website in these days. You must be very alerted by the spambots that are crawling trough your site. When I build my blog, I was very aware of this and made some simpel yet effective code to protect some of my html by using Javascript.

I see lots of website just doing mymail [at] yourwebsite [dot] com. This will proberply help for some of the spambots. But definitely not all! If I was a spambot, I would also looking for the [at]'s and [dot]'s, and also the {at}'s and {dot}'s like I do.

Here is the snippet:

 
function protect( $s )
{
	$s = str_replace( "\n", ' ', str_replace( "\r", ' ', $s ) );
 
	$a = array();
	$p = 0;
	$l = strlen( $s );
 
	while( $p < $l )
	{
		$n = rand( 1, 4 );
		$a[] = substr( $s, $p, $n );
		$p += $n;
	}
 
	asort( $a );
 
	$j = 'var a = new Array();';
 
	foreach( $a as $k => &$v ) {
		$j .= "a[$k] = '" . addslashes( $v ) . "';";
	}
 
	$j .= 'document.write( a.join("") );';
 
	return '<script language="javascript">' . $j . '</script>';
}

You can just do the following to write the protected code.

 
echo protect( 'Help! Protect me from these evil spambots! Please!' );
echo protect( '<a href="mailto:insert.mail@address.here">e-mail me</a>' );

What it does is. It cuts the inputted string into random length en put them into a array. Sort the array to make ik not linear. And let Javascript join it into a string and document.write() it. This time Javascript writing it and your code would not be seen as plain text in the html source code.

 
Tags: Development, PHP.
 
 
 
 
 
Comments 
 
Vincent on 5 Apr 2010 16:06:14 says:
Hi Edwin, thanks for the link. Only I wouldn't advise that one. The problem with just encoding entities is that it is simple to decode. In PHP you just find <a href="foo">bar</a> and then html_entity_decode(). In my method I use Javascript and randomize the array. That's very hard too read for dumb bots. I think the Google bot can still read it though ;)
 
edwin on 5 Feb 2010 16:44:42 says:
Hi Vince! You might also incorporate the technique as used here: http://www.wbwip.com/wbw/emailencoder.html Leetr