반응형
- 아마도 스팸봇들 등으로 부터 이메일을 캣치 당하는것을 방지해주는 함수인듯?

PHP hide_email()

1. What is it?

A PHP function to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail addresses for sending you spam. It uses a substitution cipher with a different key for every page load. Look at the generated XHTML in the example while pressing the browsers "reload" button to see this in effect.

2. How does it work?

PHP encrypts your E-mail address and generates the javascript that decrypts it. Most bots and spiders can't execute javascript and that is what makes this work. A visitor of your web page will not notice that you used this script as long as he/she has javascript enabled. The visitor will see "[javascript protected email address]" in stead of the E-mail address if he/she has javascript disabled.

3. Example

<?php echo hide_email('test@test.com'); ?>

This is the PHP code you write where you want the E-mail address on your web page.

test@test.com

This is what the E-mail address will look like for the visitor of your web page.

<SPAN id=e776129440>[javascript protected email address]</SPAN>
<SCRIPT type=text/javascript>/*<![CDATA[*/eval("var a=\"PM37xKSakA+8N4D@2T0ijrGlWZeudn1fOv_VtoLEBJ.gm-RIyChws6bXzpYF9cUq5QH\";var b=a.split(\"\").sort().join(\"\");var c=\"9-F949-F93gX6\";var d=\"\";for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));document.getElementById(\"e776129440\").innerHTML=\"<a href=\\\"mailto:\"+d+\"\\\">\"+d+\"</a>\"")/*]]>*/</SCRIPT>

This is the generated XHTML that the bot or spider will see instead of your E-mail address.

4. The code

The "hide_email()" PHP function is only 9 lines of code:

function hide_email($email) { $character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; $key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999); for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])]; $script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";'; $script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));'; $script.= 'document.getElementById("'.$id.'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"'; $script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")"; $script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>'; return '<span id="'.$id.'">[javascript protected email address]</span>'.$script; }

License: Public domain.

5. XHTML generator

You can use this generator if you have no PHP support on your web server. Change the E-mail address into your own E-mail address and press "Generate". Cut and paste the generated XHTML into your own web page.

E-mail address

Generated XHTML

Because the generator uses Javascript instead of PHP you can save this page to disk as "Web Page, complete" and use it offline.

6. Credits

The idea of javascript E-mail address obfuscation is not mine. It seems that Tim Williams came up with the idea first. Andrew Moulden improved it by adding a generated key. Ross Killen wrote a PHP version that generates a different key every page load. My implementation is much like that of Ross Killen, but I implemented a slightly different encryption algorithm, minified and obfuscated the javascript and made the script valid for javascript strict and XHTML 1.0 strict parsing.

  1. HTML generator by Tim Williams (University of Arizona)
  2. Improved HTML generator by Andrew Moulden (Site Engineering Ltd.)
  3. PHP version by Ross Killen (Celtic Productions Ltd.)

7. Considerations

  • Users must have javascript enabled to see your E-mail address.
  • This does not protect you against bots and spiders that can execute javascript.
  • The position of the key and the cipher text in the javascript are constant.
  • If this script gets very popular bots and spiders might get taught decoding it.
  • Line 7 of the PHP code complicates decoding (due to "eval"), but can be left out.
  • The main reason for not adding much more complexity is wanting few lines of code.
  • I chose the "span" tag over the semantically more correct "noscript" tag;
    the XHTML 1.0 strict schema says the "noscript" tag may only contain "Block" elements.

8. Interesting links

Send me your ideas and comments on this subject!

Maurits van der Schee - maurits@vdschee.nl - August 2008 - Amsterdam - Valid XHTML 1.0 Strict


반응형

'Web > PHP' 카테고리의 다른 글

참조연산자 (&)  (0) 2010.04.29
php 4.x 버전과 php 5.x 버전 동시에 사용하기[리눅스 설정]  (0) 2010.04.01
cURL  (0) 2010.01.20
간단한 달력 소스  (0) 2009.12.23
함수가 선언됐는지 여부를 알아보는 PHP 내장함수  (0) 2009.12.18

+ Recent posts