Zend - The PHP Company




Security

Add Code


Email Protector  

Type: code fragment
Added by: xpc
Entered: 12/03/2003
Last modified: 03/12/2002
Rating: - (fewer than 3 votes)
Views: 9363
If you want to use PHP to display emails without fear of spambots taking them for ill-use, when you don't have an encryption algorithm, here is a simple and usefull tool to both hide and present emails with sessions and random encoding. This is how you can easily encode your emails on a webpage. This works by generating a unique password to attach to each email being encoded per page in a random place. To see a error, copy and paste a email link into a new window.


<?

/***

* PHP email display and call by Michael Glazer http://coding.4arrow.com March 11, 2003

* Let's Roll!
* America Rules!
*
* You can freely use this script as long as this header remains intact
*
**/

 // start session
session_start ( );


 
// request being made
if ( $argv[0] ) {

     
// not allowed a direct request
    
if ( $_SESSION['page'] != $_SERVER['HTTP_REFERER'] ) die ( 'Not Allowed!' );

     
// decode request
    
$decode base64_decode $argv[0] );

    
// strip our hidden random password
    
$email    =    str_replace $_SESSION['key'] , '' $decode );

    
// open mailto link and return to previous page then die
    
die ( '<script>location.href='mailto:'.$email.''; location.href=''.$_SESSION['page'].''</script>' );

}

 
// our array of emails
$emails = array('test1@yahoo.com','test2@yahoo.com');

// current page beign requested
$_SESSION['page']    =    'http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// random generated password to append to a random place within each email
$_SESSION['key']    =    md5(microtime().posix_getpid());

// iteriate through each email append random key to random place in email
foreach ( $emails as $k => $email ) {

    
// encode email
    
$encode base64_encode posKey $email $_SESSION['key'] ) );

    
// write email
    
echo '<a href="emails.php?'.$encode.'">Email '.$k.'</a><br>'.chr(13);

}

// function to randomlly position our random password to each email
function posKey $email $key ) {

    
$sep1    =    '@';
    
$sep2    =    '.';

    
$FS    =    explode $sep1 $email );

    
$ST    =    explode $sep2 $FS[1] , );

    
$F    =    $FS[0]; // first

    
$S    =    $ST[0]; // second

    
$T    =    $ST[1]; // third

    
$place    =    array('FB','FM','FE','SB','SM','SE','TB','TM','TE');

    
$placement    =    $place[rand(0sizeof($place) -1)];


    switch ( 
$placement ) {

        case 
'FB'//first beginning

            
return $key $email;

            break;

        case 
'FM'// first middle

            
$len    =    strlen ($F);

            
$half    =    ($len 2);

            
$front    =    substr($F,0,$half);

            
$back    =    substr($F,$half);

            return 
$front $key $back $sep1 $FS[1];

            break;

        case 
'FE'// first ending

            
return $F $key $sep1 $FS[1];

            break;

        case 
'SB'// second beginning

            
return $F $sep1 $key $FS[1];

            break;

        case 
'SM'// second middle

            
$len    =    strlen ($S);

            
$half    =    ($len 2);

            
$front    =    substr($S,0,$half);

            
$back    =    substr($S,$half);

            return 
$F $sep1 $front $key $back $sep2 $T;

            break;

        case 
'SE'// second ending

            
return $F $sep1 $S $key $sep2 $T;

            break;

        case 
'TB'// third beginning

            
return $F $sep1 $S $sep2 $key $T;

            break;

        case 
'TM'// third middle

            
$len    =    strlen ($T);

            
$half    =    ($len 2);

            
$front    =    substr($T,0,$half);

            
$back    =    substr($T,$half);

            return 
$F $sep1 $S $sep2 $front $key $back;

            break;

        case 
'TE'// third ending

            
return $email $key;

            break;

    }

}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories