Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Goto page: 1 - 2 [+1]

Bottom
[Tutorial] Encode email addresses - keep 'em safe from bots!
  • Posted: 07.04.2005, 17:59
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Now, first of all, if you're writing your module in pnRender, there's no need for this, as pnRender already has hex encoding capabilities built-in. That said, if you're writing a module in pnHTML and you want to encode your email addresses so that harvesters/bots can't get them, this quick function is just what you need. This "tutorial" is really more of a stand-alone-based function, but can be easily integrated into your pnHTML modules to be used in the pnuserapi.php file, for instance. Anyway...moving right along then...

    In case you have no idea what I'm talking about, "hex encoding an email address" is a method by which you thwart email-harvesters/bots from slurping your email address off your pages by encoding each text character into it's equivalent hex value. Basically, you're "disguising" it. In other words, an email address such as:

    Code

    YourMom@WhateverSiteSheUses.com
    would be converted into this:

    Code

    %59%6f%75%72%4d%6f%6d%40%57%68%61%74%65%76%65%72%53%69%74%65%53%68%65%55%73%65%73%2e%63%6f%6d
    ... neat trick, yes? ...So now that it's converted, it could be used in an email link with a "reasonable" amount of protection like this:

    Code

    <a href="mailto:%59%6f%75%72%4d%6f%6d%40%57%68%61%74%65%76%65%72%53%69%74%65%53%68%65%55%73%65%73%2e%63%6f%6d">Contact Your Mom!</a>


    So this is all fine and dandy...but who's got the time and patience to encode email addresses by hand?!? Yea, me either. So, I've written a short function that allows you to feed it a typical email address (or any other text, for that matter) and it neatly pops out the hex-encoded equivalent to be used in a link. Keep in mind that this function is not pnHTML, or even PostNuke, specific...you can use it in any script you might have that needs email encoding. Ready?? Here we go...

    First order of business, I assume that you've got an email address that you want to output to the browser. How you obtain it is irrelevent, whether you call it directly out of your module table or loop through an array...it doesn't matter...just so long as it's there. For the sake of this "tutorial" we'll say that your module has the equivalent of the following variable at your disposal:
    $email = 'YourMom@WhateverSiteSheUses.com';

    So...now here's the encoding function...

    Code

    function dohex($email)
    {
      if($email == '') // if $email is empty, simply return.
        return;
      else // or else, if $email is not empty, proceed.
      {
        $length = strlen($email); // get the number of characters in the email address
        for($i=0;$i<$length;$i++) // loop through, encoding each character individually.
        {
          $x = substr($email,$i,1); // assign each letter as $x as we proceed.
          $hexMail .= "%".dechex(ord($x)); // build the encoded email by adding each $x to $hexMail, one at a time.
        }
      }
      return $hexMail; // finally return the entire encoded email address.
    }


    As you can see, it's pretty simple...but obviously, writing the function out simply states what it should do...it doesn't actually do anything until you call it. So you call the function like this:

    Code

    $hexMail = dohex($email);
    ...and what it does is takes your $email, encodes each letter as an $x, and then successively adds each $x value to $hexMail variable, one by one, until finally it's got a fully encoded string to hand back to you for use in your email link. (I say "finally", but the entire process takes milliseconds in real life.)

    So, there's only one thing left to do and that is to use $hexMail in an email link. Keeping in mind that the format of an email link is:

    Code

    <a href="mailto:[EMAILADDRESSHERE]">Text</a>
    ...you just insert the encoded value where the email address is supposed to go. So, using our now-encoded $email (which is $hexMail) the email link goes like this (for instance):

    Code

    echo '<a href="mailto:'.$hexMail.'">Contact me!</a>';


    So, using this tiny function in your module can afford your email addresses a bit of protection against harvesting. Below is the entire code, so that you can get a better idea of how it works together. Whether you use the code in the pnuserapi.php file or the pnuser.php file is up to you really...I just wanted to provide you with the function to get the job done.

    Code

    // This variable would actually come from your module, rather than setting it here.  This is just an example.
    $email = 'YourMom@WhateverSiteSheUses.com';

    // This is the actual function that does all the work
    function dohex($email)
    {
      if($email == '')
        return;
      else
      {
        $length = strlen($email);
        for($i=0;$i<$length;$i++)
        {
          $x = substr($email,$i,1);
          $hexMail .= "%".dechex(ord($x));
        }
      }
           
      return $hexMail;
    }
       
    // This is the generic function call.
    $hexMail = dohex($email);
       
    // This is the encoded email link.
    echo '<a href="mailto:'.$hexMail.'" title="Contact Me">Contact me!</a>';


    So, that's it! It's really just a quick thing that I dragged out with a bunch of explanatory text. Hopefully, you can find some use in it. This post is based on a tutorial I'm writing for my site...your feedback and comments are welcome.

    --
    Photography | PHP | Other
  • Posted: 07.04.2005, 18:07
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    I'll attempt to add another post to this thread detailing the exact PostNuke-specific procedure so that it'll take a bit of the guesswork out of it, but I can't guarantee it, or say a timeframe...so, if you're feeling froggy, don't be afraid to make it PN-centric!

    --
    Photography | PHP | Other
  • Posted: 07.04.2005, 19:29
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    It can already be done in postnue in either a pnRender or Xanthia template - see the smarty mailto plugin. The equivalent to your example would be

    Code

    <!--[mailto address="YourMom@WhateverSiteSheUses.com" encode="hex"]-->


    -Mark
  • Posted: 07.04.2005, 19:30
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Yes, that's why my first sentence says that... ;)

    --
    Photography | PHP | Other
  • Posted: 07.04.2005, 19:32
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720

    alarconcepts

    Yes, that's why my first sentence says that... ;)


    Whoops icon_redface I missed that..... That'll teach me to just read the code.

    -Mark
  • Posted: 07.04.2005, 19:34
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    There's a good deal of us who aren't up to pnRender'ing yet... I promise I'll get with the program in time. Just trying to offer up a solution that I thought might be useful to some of us slackers...

    :)

    --
    Photography | PHP | Other
  • Posted: 07.04.2005, 19:37
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720

    alarconcepts

    There's a good deal of us who aren't up to pnRender'ing yet... I promise I'll get with the program in time. Just trying to offer up a solution that I thought might be useful to some of us slackers...

    :)


    Very useful.... I just fell into a developers mistake - reading the code and assuming that the words around it were superfluous.

    -Mark
  • Posted: 07.04.2005, 19:39
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Oh...make no mistake...to a developer, much of what I have to say IS superfluous. Much of what I write is geared toward newbies and not-quite-novices...

    --
    Photography | PHP | Other
  • Posted: 09.04.2005, 04:16
     
    InvalidResponse
    rank:
    Professional Professional
    registered:
     September 2003
    Status:
    offline
    last visit:
    21.10.07
    Posts:
    2423
    ..so long as the next tutorial isn't in Assembler or Cobol ;)

    superfluous..superfluous..superfluous..superfluous..superfluous... I'm making that my word of the day. :D

    --
    http://www.invalidresponse.com
  • Posted: 09.04.2005, 04:22
     
    csteelatgburg
    rank:
    Helper Helper
    registered:
     October 2002
    Status:
    offline
    last visit:
    23.01.07
    Posts:
    192
    Then again, just saying that pnRender has a hex encoding function built in, and actually showing someone how to use it are completely different things.
  • Posted: 09.04.2005, 06:39
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723

    InvalidResponse

    ..so long as the next tutorial isn't in Assembler or Cobol ;)

    superfluous..superfluous..superfluous..superfluous..superfluous... I'm making that my word of the day. :D


    Smartass :!:

    Guess I can nix 2 of my upcoming ideas then...

    ...and to think, I had planned a fantastically baroque 15,000 word explanation on how to press the enter key...followed by a poignant 6-part pay-per-view on how to sharpen a pencil... hmph... Maybe I'll define "sticks in the mud" instead... icon_lol You guys are no fun...

    Thanks for the vote of confidence Chuck...

    --
    Photography | PHP | Other
  • Posted: 09.04.2005, 21:19
     
    csteelatgburg
    rank:
    Helper Helper
    registered:
     October 2002
    Status:
    offline
    last visit:
    23.01.07
    Posts:
    192
    Oh, I enjoyed the tutorial (and even read all of the text). I'm just still new to pnRender so had no idea that it could handle that problem so quickly and elegantly. I would have dove through the documentation (or code if need be) to see how, after your comment, but seeing it right there was much easier, since I'm lazy and all.

    More simple tutorials like this are exactly what we need to keep PostNuke going.

    Well, that and teaching people to search for a solution before posting...or read the documentation...or the FAQs.
  • Posted: 09.04.2005, 22:21
     
    Simon
    rank:
    Steering Committee Steering Committee
    registered:
     December 2002
    Status:
    offline
    last visit:
    09.11.08
    Posts:
    13413

    Quote


    Very useful.... I just fell into a developers mistake - reading the code and assuming that the words around it were superfluous.


    Worryingly enough, I did the same thing....

    --
    Regards,
    Simon

    itbegins.co.uk - Zikula Consulting

    Please read the Support Guide
  • Posted: 09.04.2005, 22:35
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    @Simon ...maybe you're just too used to my long-winded posts...

    @Chuck ...I wish I could operate a "smack button"...a device that smacks those who post without searching first...

    --
    Photography | PHP | Other
  • Posted: 09.04.2005, 23:59
     
    InvalidResponse
    rank:
    Professional Professional
    registered:
     September 2003
    Status:
    offline
    last visit:
    21.10.07
    Posts:
    2423

    alarconcepts

    Smartass :!:

    Guess I can nix 2 of my upcoming ideas then...

    ...and to think, I had planned a fantastically baroque 15,000 word explanation on how to press the enter key...followed by a poignant 6-part pay-per-view on how to sharpen a pencil... hmph... Maybe I'll define "sticks in the mud" instead... icon_lol You guys are no fun....

    hahhaha.. well at least someone's got a sense of humor :) .. of course it's useful.. and well written :)

    --
    http://www.invalidresponse.com

Goto page: 1 - 2 [+1]

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula