Back to examples |
Its not an easy job to hide an email address on a web-page. Spammers and hackers can obtain email addresses from a web page just by scanning the source code for the relevant markers. Even if you use Java script, the address is still in the source code and could be extracted by an automated system - and of course your code relies on the client PC having java script enabled.
Even though as a last resort you can hide or disguise an email address by simply renaming some of the key elements - ie "test at thisdomain dot com", the below example allows you to display a full email address in a document by creating an image containing the email as text.
To provide greater resilience against email harvesting scripts, the code contains 2 parts. The first part encrypts the email address and creates a valid <IMG> tag containing the link to the second part of the code which decrypts the address and displays it. This code therefore also contains a very tidy private / public key encryption class ( which I do not proclaim to have created ) which could be used in a wide number of other systems.
Obviously this code does not stop someone just reading the address and writing it down, or prevent any OCR type scanning, however, it is very handy for hiding other URLS or text which you do not want access by bots or scripts - for example a confirmation code or similar.
|
<?php
class cls_encrypt { var $_key;
function cls_encrypt() { $this->_key = "php_example"; // This is the private key return 1; }
function keyED($txt) { $encrypt_key = md5($this->_key); $ctr=0; $tmp = ""; for ($i=0;$i<strlen($txt);$i++) { if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); $ctr++; } return $tmp; }
function html_encrypt ($txt) { return urlencode($this->encrypt($txt)); }
function encrypt($txt) { $encrypt_key = md5("public_key"); // Public key $ctr=0; $tmp = ""; for ($i=0;$i<strlen($txt);$i++) { if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); $ctr++; } return $this->keyED($tmp); }
function html_decrypt($txt) { return $this->decrypt(urldecode($txt)); }
function decrypt($txt) { $txt = $this->keyED($txt); $tmp = ""; for ($i=0;$i<strlen($txt);$i++) { $md5 = substr($txt,$i,1); $i++; $tmp.= (substr($txt,$i,1) ^ $md5); } return $tmp; } }
if(!empty($email)) { $email = $HTTP_GET_VARS["email"];
$enc = new cls_encrypt; $email = $enc->decrypt($email);
$font = 2; // Change this to whatever you want
$im = imagecreate(imagefontwidth($font)*strlen($email), imagefontheight($font));
$black = imagecolorallocate($im, 0x00, 0x00, 0x00); $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
imagefill($im,0,0,$white); imagestring($im, $font,0,0,$email,$black); header("Content-type: image/png"); imagepng($im); imagedestroy($im); } else { $enc = new cls_encrypt;
$this_file = ??
// change $this_file to whatever the decode script is going to be called. // For this example we use the same code, we just enter a different part // depending on whether $email is parsed.
// You may want to split the code into 2 different scripts, one for display // the other for getting the encoded URL for the email address
// This is the email address we are going to encode. // Again, you can do this is another script just copy or include // the encrypt class
$email = $enc->encrypt("notreal@anydomain.com"); print("<img src=\"".$this_file."?email=".urlencode($email)."\">"); }
?>
|
|
|
misc_4.zip
|