Page 1 of 1

email address encoder needed

Posted: Wed Apr 04, 2007 7:26 am
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

Posted: Wed Apr 04, 2007 7:43 am
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);

uh.. where's the 'Undo Button'

Posted: Wed Apr 04, 2007 8:15 am
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

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

Posted: Wed Apr 04, 2007 8:23 am
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);
}

Thanks!!!

Posted: Wed Apr 04, 2007 9:48 am
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

Re: Thanks!!!

Posted: Wed Apr 04, 2007 10:04 am
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.