If we place direct email address in our webpages hackers spombots will read the email addresses and hackers will send automated spam emails to these email addresses.
These public email addresses will receive lots of automated spam emails every day.
To prevent this effect we have to use HTML encrypted string or URL encrypted string in the place of direct email address.
For example: If we place below HTML tag in the website,
In anchor link we should use URL encrypted string and in navigation text we should place HTML encrypted text.
So our updated HTML script will be similar t below:
<a href="m%61i%6c%74o:%74e%73t%65%6da%69l%64e%6da%69%6c%2ecom">testemail@email.com</a>
If we place above script in HTML file output will be our normal URL.
We can use below PHP functions to convert email address to encrypted string.
<?php
/**
* Function to return url/html encrypted email address
*
* @param string $emailId
* @param string $displayType, values should be text or url
* @return string encrypted email address
*/
function makeSpambotFree($emailId, $displayType="text") {
$output = "";
$displayType = ($displayType == "url") ? "url" : "text";
srand(microtime() * 1000000);
if($displayType == "text") { // for displaying the email address in web page
for ($i = 0; $i < strlen($emailId); $i = $i + 1) {
$j = floor(rand(0, 1));
if ($j==0) {
$output .= '&#'.ord(substr($emailId,$i,1)).';';
} elseif ($j==1) {
$output .= substr($emailId,$i,1);
}
}
$output = str_replace('@','@',$output);
} else { // for passing email address in anchor link
for ($i = 0; $i < strlen($emailId); $i = $i + 1) {
$j = floor(rand(0, 1));
if ($j==0) {
$output .= substr($emailId,$i,1);
} elseif ($j==1) {
$output .= '%'.padZeros(dechex(ord(substr($emailId, $i, 1))), 2);
}
}
$output = str_replace('@','%64',$output);
}
return $output;
}
/**
* Function to return url/html encrypted email address
*
* @param string $emailId
* @param string $displayType, values should be text or url
* @return string encrypted email address
*/
function makeSpambotFree($emailId, $displayType="text") {
$output = "";
$displayType = ($displayType == "url") ? "url" : "text";
srand(microtime() * 1000000);
if($displayType == "text") { // for displaying the email address in web page
for ($i = 0; $i < strlen($emailId); $i = $i + 1) {
$j = floor(rand(0, 1));
if ($j==0) {
$output .= '&#'.ord(substr($emailId,$i,1)).';';
} elseif ($j==1) {
$output .= substr($emailId,$i,1);
}
}
$output = str_replace('@','@',$output);
} else { // for passing email address in anchor link
for ($i = 0; $i < strlen($emailId); $i = $i + 1) {
$j = floor(rand(0, 1));
if ($j==0) {
$output .= substr($emailId,$i,1);
} elseif ($j==1) {
$output .= '%'.padZeros(dechex(ord(substr($emailId, $i, 1))), 2);
}
}
$output = str_replace('@','%64',$output);
}
return $output;
}
/**
* For returning url encrypted character
*
* @param int $number
* @param int $length
* @return return url encrypted string
*/
function padZeros($number, $length) {
return sprintf('%0'.$length.'s', $number);
}
// Usage
echo makeSpambotFree("testemail@email.com"); // for text
echo makeSpambotFree("mailto:testemail@email.com", "url"); // for anchor url
?>
* For returning url encrypted character
*
* @param int $number
* @param int $length
* @return return url encrypted string
*/
function padZeros($number, $length) {
return sprintf('%0'.$length.'s', $number);
}
// Usage
echo makeSpambotFree("testemail@email.com"); // for text
echo makeSpambotFree("mailto:testemail@email.com", "url"); // for anchor url
?>
No comments:
Post a Comment