<pedrocorreia.net ⁄>
corner
<mySearch ⁄> <mySearch ⁄>

corner
 
corner
<mySnippets order="rand" ⁄> <mySnippets order="rand" ⁄>

corner
 
corner
<myContacts ⁄> <myContacts ⁄>

<email ⁄>


pc@pedrocorreia.net

<windows live messenger ⁄>


pedrojacorreia@hotmail.com

<myCurriculum type="pdf" ⁄>


Download
corner
 
corner
<myBlog show="last" ⁄> <myBlog show="last" ⁄>

corner
 
corner
<myNews show="rand" ⁄> <myNews show="rand" ⁄>

corner
 
corner
<myNews type="cat" ⁄> <myNews type="cat" ⁄>

corner
 
corner
<myQuote order="random" ⁄> <myQuote order="random" ⁄>

corner
 
corner
<myPhoto order="random" ⁄> <myPhoto order="random" ⁄>

<pedrocorreia.net ⁄>
corner
 
corner
<myAdSense ⁄> <myAdSense ⁄>

corner
 
corner
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

corner
 
 

<Possible methods to protect e-mail address from spam bots ⁄ >




clicks: 10309 10309 2008-06-10 2008-06-10 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This



Welcome to my first snippet ... in English. From now on I'll start writing more in English, but I won't stop writing a few more snippets in my main language, Portuguese. Since English's not my main language, certainly I'll give some errors, I hope that'll be just a few ;)


For my first English snippet I'll try to demonstrate some possible ways to protect your e-mail from those evil spam bots :)


It'll a very simple way to do, since the spam bots read the html page source, let's try to change the way that some data (in this case emails, but it can be to many other things) appears.



Basically the trick here is to play with ord. Quoting the definition of ord from PHP manual page it states: "... Returns the ASCII value of the first character of string ...", this means that we'll iterate over our string and char by char we'll get his Ascii code (is just a numeric value), but since Ascii code's just a "normal" number, html will see it just as it, a normal number, so we'll just have to add the following 3 chars "&#;" (without quotes), so that Html parser will know how to interpret that char, this is called character entities, you can get more information in this link

I wrote a fairly simple PHP5 class, but porting this code to PHP4 is easy, even for other languages such as ASP, ASP.NET, etc, etc.

Here goes the code and examples:

- class.ProtectEmail.php
  1. <?php
  2. /**
  3. * Class ProtectEmail
  4. *
  5. * This class implements a few possible methods
  6. * to protect e-mail addresses from spam bots
  7. *
  8. * @author pedrocorreia.net
  9. */
  10. class ProtectEmail{
  11. private $_email;
  12.  
  13. /**
  14. * Constructor
  15. *
  16. * @param String E-mail
  17. */
  18. public function __construct($email){
  19. if($this->_IsValid($email)) $this->_email=$email;
  20. else throw new Exception("EMAIL_IS_NOT_VALID");
  21. }
  22.  
  23. /**
  24. * "Beautify" e-mail by converting symbols to strings
  25. *
  26. * @return String
  27. */
  28. public function Beautifier(){
  29. if(!$this->_email) throw new Exception("EMAIL_NOT_SPECIFIED");
  30. $search_for=array("/\./","/@/","/-/");
  31. $replace_with=array(" Dot "," At ", " Hyphen ");
  32.  
  33. return $this->Encoder(preg_replace($search_for,$replace_with,$this->_email));
  34. }
  35.  
  36. /**
  37. * Encode all elements in a String to their ascii codes
  38. *
  39. * note: if no argument's specified, the string encoded
  40. * will be the attribute defined in the constructor
  41. *
  42. * @param String [Optional]
  43. * @return String
  44. */
  45. public function Encoder($email=""){
  46. if(!$email) $email=$this->_email;
  47. if(!$email) throw new Exception("EMAIL_NOT_SPECIFIED");
  48.  
  49. $count=strlen($email);
  50. $encoded_str=array();
  51. for ($i = 0; $i < $count; $i++){
  52. $char=ord($email{$i});
  53. $encoded_str[$i] = "&#$char;";
  54. }
  55. return implode("",$encoded_str);
  56. }
  57.  
  58. /**
  59. * Check if is a valid e-mail address
  60. *
  61. * note: complete credits to http://www.ilovejackdaniels.com/php/email-address-validation/
  62. *
  63. * @param String
  64. * @return String
  65. */
  66. private function _IsValid($email) {
  67. // First, we check that there's one @ symbol, and that the lengths are right
  68. if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
  69. // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
  70. return false;
  71. }
  72. // Split it into sections to make life easier
  73. $email_array = explode("@", $email);
  74. $local_array = explode(".", $email_array[0]);
  75. for ($i = 0; $i < sizeof($local_array); $i++) {
  76. if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
  77. return false;
  78. }
  79. }
  80. if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
  81. $domain_array = explode(".", $email_array[1]);
  82. if (sizeof($domain_array) < 2) {
  83. return false; // Not enough parts to domain
  84. }
  85. for ($i = 0; $i < sizeof($domain_array); $i++) {
  86. if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93.  
  94. }
  95.  
  96. ?>








Here are some examples:
  1. <?php
  2. include_once("class.ProtectEmail.php");
  3.  
  4. $email="email@my-domain.com.pt";
  5.  
  6. try{
  7. $protected_email=new ProtectEmail($email);
  8. }
  9. catch (Exception $ex){
  10. echo $ex->getMessage();
  11. return ;
  12. }
  13.  
  14. //we have a valid object
  15. if($protected_email){
  16. try{
  17. $email_encoded=$protected_email->Encoder();
  18. }
  19. catch (Exception $ex){
  20. echo $ex->getMessage();
  21. }
  22.  
  23. try{
  24. $email_beautified=$protected_email->Beautifier();
  25. }
  26. catch (Exception $ex){
  27. echo $ex->getMessage();
  28. }
  29.  
  30. $email_link="<a href='mailto:$email_encoded'>$email_beautified</a>";
  31.  
  32. echo "$email_encoded<br/><br/>$email_beautified<br/><br/>$email_link";
  33. }
  34.  
  35. ?>




This is what we'll see in the browser:

email@my-domain.com.pt

email At my Hyphen domain Dot com Dot pt

email At my Hyphen domain Dot com Dot pt



Now if we choose to view html page source, we'll stumble upon the following hieroglyphs ^_^''


&#101;&#109;&#97;&#105;&#108;&#64;&#109;&#121;&#45;&#100;&#111;
&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;&#46;&#112;&#116;
<br/><br/>
&#101;&#109;&#97;&#105;&#108;&#32;&#65;&#116;&#32;&#109;&#121;
&#32;&#72;&#121;&#112;&#104;&#101;&#110;&#32;&#100;&#111;
&#109;&#97;&#105;&#110;&#32;&#68;&#111;&#116;&#32;&#99;&#111;
&#109;&#32;&#68;&#111;&#116;&#32;&#112;&#116;
<br/><br/>
<a href='mailto:&#101;&#109;&#97;&#105;&#108;&#64;&#109;&#121;
&#45;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;
&#46;&#112;&#116;'>&#101;&#109;&#97;&#105;&#108;&#32;&#65;&#116;
&#32;&#109;&#121;&#32;&#72;&#121;&#112;&#104;&#101;&#110;&#32;
&#100;&#111;&#109;&#97;&#105;&#110;&#32;&#68;&#111;&#116;&#32;
&#99;&#111;&#109;&#32;&#68;&#111;&#116;&#32;&#112;&#116;</a>




If you have any doubt or found any error, please drop me an email









clicks: 10309 10309 2008-06-10 2008-06-10 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This