Zend - The PHP Company




Email

Add Code


Anti-Spam Email Disguiser  

Type: code fragment
Added by: Glen
Entered: 31/07/2002
Last modified: 07/12/2006
Rating: **** (5 votes)
Views: 11533
I find it necessary to obscure my email addresses. This code was inspired by a script at http://www.healyourchurchwebsite.com/


<?php
//This code should help conceal the email addresses you list on your site from spammers. It's especially useful for email addresses drawn from a database (like in a phonebook application)

//obfuscate takes a string and replaces most characters with the hexidecimal ordinal equivalent. Note: return string is almost certainly much longer than the original email address
function obfuscate($email) {
    
$i=0;
    
$obfuscated="";
    while (
$i<strlen($email)) {
       if (
rand(0,2)) {
          
$obfuscated.='%'.dechex(ord($email{$i}));
       } else {
          
$obfuscated.=$email{$i};
       }
       
$i++;
   }
return 
$obfuscated;
}

//obfuscate_numeric takes a string and replaces the characters with html entity eqivalents. You have to do this if you want to obfuscate the label and have it display normally, or if you just want to obfuscate the "mailto" tag. Note: return string is almost certainly much longer than the plaintext string
function obfuscate_numeric($plaintext) {
    
$i=0;
    
$obfuscated="";
    while (
$i<strlen($plaintext)) {
       if (
rand(0,2)) {
          
$obfuscated.='&#'.ord($plaintext{$i});
       } else {
          
$obfuscated.=$plaintext{$i};
       }
       
$i++;
   }
return 
$obfuscated;
}

//this function drives the two above and generates a mailto link. if label isn't set, it just re-obfuscates the email address and uses that as the label
function generate_obfuscated_mailto ($email,$label) {
if (isset(
$label)) {
           return 
sprintf("<a href='%s:%s'>%s</a>",
           
obfuscate_numeric('mailto'),
           
obfuscate($email),
           
obfuscate_numeric($label));
} else {
           return 
sprintf("<a href='%s:%s'>%s</a>",
           
obfuscate_numeric('mailto'),
           
obfuscate($email),
           
obfuscate_numeric($email));
    }
}
?>


Usage Example


<?php
generate_obfuscated_mailto
("billg@microsoft.com","click here to ask for a loan");
generate_obfuscated_mailto("billg@microsoft.com");
?>


Rate This Script





Search



This Category All Categories