email address encoder needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

email address encoder needed

Post by VmusicV »

Hi.
Uh... I sincerely apologize if this is the wrong board - I tried and tried and tried and tried and....ok you get it.... to find a code snippet that 'encodes' and 'decrypts' email addresses.
I would have thought in this day and age you could find any piece of code in about 12 seconds........but not so.

The idea is that you first encode an email address that you will use in a mailer script from a contact form. You store the encoded string so that bots can't easily pickup your email addresss. Then in the mailer script you would call this piece of code to 'decrypt' the characters into the email addresss, so that the address is only present in memory.


Sorry if there's something famous out there that I don't know about.... but I looked for a 1/2 hour yesterday with no luck

Codeless in Columbus
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The easiest is to encode to HTML entities.

Code: Select all

function encode_address($addr)
{
    $output = "";
    for ($i = 0; $i < strlen($addr); $i++) {
        $output .= sprintf("&#x%X;", ord($addr{$i}));
    }
    return $output;
}

$address = "foo@bar.com";

echo encode_address($address);
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

uh.. where's the 'Undo Button'

Post by VmusicV »

Hi,
Now exactly how do I 'decrypt' or 'decode' this puppy?

Once I store the value of the varialbe $output - on the form, the php code will need to 'decode' or 'decrypt' this string back into the email addresss

???????????????????????


Codeless in Columbus
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: uh.. where's the 'Undo Button'

Post by Chris Corbyn »

VmusicV wrote:Hi,
Now exactly how do I 'decrypt' or 'decode' this puppy?

Once I store the value of the varialbe $output - on the form, the php code will need to 'decode' or 'decrypt' this string back into the email addresss

???????????????????????


Codeless in Columbus
Did you actually run this in your browser? That's just HTML so the browser treats it as "foo@bar.tld" just the same as the string form. So if you echo it into a form field or something, when the form is submitted you'll get "foo@bar.tld" even if the markup is all HEX.

But in any case, this will decode if you still need to know:

Code: Select all

function decode_addr($addr)
{
    return preg_replace('/&#x([A-F0-9]{2});/e', ' chr(0x$1); ', $addr);
}
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

Thanks!!!

Post by VmusicV »

Hi,
Thank you so much. So what I will do is first figure out the encoded value for the string, and I will have that in my output html - so any bot can't directly see the email address. Then in the mailer script - I'll pass that string to the decode function to return back the actual email address.

Thanks So Much
Vmusic
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Thanks!!!

Post by Chris Corbyn »

VmusicV wrote:Hi,
Thank you so much. So what I will do is first figure out the encoded value for the string, and I will have that in my output html - so any bot can't directly see the email address. Then in the mailer script - I'll pass that string to the decode function to return back the actual email address.

Thanks So Much
Vmusic
You should never need to decode it. The browser will do that. Try it.
Post Reply