Php Programming Code Examples
Php > E-Mail Code Examples
Protect your mailto email addresses from bots - pure PHP
Protect your mailto email addresses from bots - pure PHP
After reading an example of how to secure your email on your website from robots, which was incorrect, as it was pure PHP, I decided to give you my function for doing it.
The code below takes 1 or 2 parameters, the email and the text for the link. If you omit the text, the email address is used.
<?php
function secure_email($email, $name=null)
{
$email_parts=split("@",$email);
$email_parts[1]=str_replace(".", ".", $email_parts[1]);
if(is_null($name))
{
$name='\' + user + \'@\' + site + \'';
}
else
{
// change ' to \' to prevent messing up javascript, dont addslashes as only ' needs to be converted
$name=str_replace("'", "\'", $name);
}
$code="";
$code.='<script language="JavaScript">' . "\n";
$code.='<!-- Begin user' . "\n";
$code.='user = "' . $email_parts[0] . '";' . "\n";
$code.='site = "' . $email_parts[1] . '";' . "\n";
$code.='document.write(\'<a href=\\"mailto:\' + user + \'@\' + site + \'\\">\');' . "\n";
$code.='document.write(\'' . $name . '</a>\');' . "\n";
$code.='// End -->' . "\n";
$code.='</script>' . "\n";
return $code;
}
?>