In the context of PHP development, what are some alternative methods to using "mailto" links for email addresses in a web application?
Using "mailto" links for email addresses in a web application can expose the email address to spam bots, leading to an increase in unwanted emails. An alternative method is to use PHP to obfuscate the email address by converting it into a JavaScript function that dynamically generates the email address on the client-side. This helps protect the email address from being easily harvested by spam bots.
<?php
function obfuscate_email($email) {
$email_parts = str_split($email, 1);
$obfuscated_email = '';
foreach ($email_parts as $part) {
$obfuscated_email .= '&#'.ord($part).';';
}
return $obfuscated_email;
}
$email = 'example@example.com';
$obfuscated_email = obfuscate_email($email);
?>
<script>
document.write('<?php echo $obfuscated_email; ?>'.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
}));
</script>
Related Questions
- What are the differences between using file_exists() and curl in PHP to check for file existence and perform redirects?
- What are the best practices for using setcookie function in PHP to avoid errors?
- How does the use of a template engine like Laravel affect the inclusion of templates compared to using traditional PHP includes?