Solved

PHP: How can i trigger the Event "WebPage Prerender" within my php-program

Posted on 2010-11-18
3
334 Views
Last Modified: 2012-05-10
Wrote a huge PHP-Program years ago. Now i want to obfuscate all e-Mail addresses on my pages. Plan to do it as follows:
>>Event "Prerender"
1. replace all plaintext emails and email links with span elements.
2.Encript and store email addresses in javascript variables.
3. Javascript decryption routines are triggered by the browser on 'window.onload' event and replace the span elements with the real email addresses.
There is only ASP examples with Event-Handling.
0
Comment
Question by:Solenthaler
3 Comments
 
LVL 15

Accepted Solution

by:
AndyBeard earned 250 total points
Comment Utility
I actually have a php class intended to convert links with nofollow to javascript that I haven't released yet, but the code does most of what you want.

The PHP converts links into a span with a html5 data element which is encrypted based upon a key you set, and there is a corresponding javascript file to decrypt.

It doesn't rely on any other javascript library, and I use it async.

I have actually been running it live mainly in the comment section of my blog (http://andybeard.eu) for the last few month.

If you want a copy of the code just contact me via my contact form on my blog.

The code is free... I just haven't decided how free - part is based of of some MIT licenced code, but I am going to also release it as a WP plugin (which I also have running) which in theory means at least some of the code, maybe all has to be GPL.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
If you want to obscure email addresses, this example might do the trick.
<?php // RAY_obscure_email.php

error_reporting(E_ALL);





// DEMONSTRATE HOW TO ENCODE EMAIL ADDRESSES TO REDUCE 'BOT HARVESTING





// RETURN OBFUSCATED STRING

function obscure($raw)

{

    $chrs = str_split(trim($raw));

    $done = '';

    for ($k=0; $k < count($chrs); $k++)

    {

        $done.='&#'.ord($chrs[$k]).';';

    }



    // RANDOM UPPERCASE CHANGES

    $nums = range(65,90);

    $chrs = range('A', 'Z');

    $ran1 = rand(65,90);

    $ran2 = rand(65,90);

    $ran3 = rand(65,90);

    $done = str_replace('&#' . "$ran1" . ';', $chrs[$ran1-65], $done);

    $done = str_replace('&#' . "$ran2" . ';', $chrs[$ran2-65], $done);

    $done = str_replace('&#' . "$ran3" . ';', $chrs[$ran3-65], $done);



    // RANDOM LOWERCASE CHANGES

    $nums = range(97,122);

    $chrs = range('a', 'z');

    $ran1 = rand(97,122);

    $ran2 = rand(97,122);

    $ran3 = rand(97,122);

    $done = str_replace('&#' . "$ran1" . ';', $chrs[$ran1-97], $done);

    $done = str_replace('&#' . "$ran2" . ';', $chrs[$ran2-97], $done);

    $done = str_replace('&#' . "$ran3" . ';', $chrs[$ran3-97], $done);



    // RANDOM NUMERIC CHANGES

    $nums = range(48,57);

    $chrs = range('0', '9');

    $ran1 = rand(48,57);

    $ran2 = rand(48,57);

    $ran3 = rand(48,57);

    $done = str_replace('&#' . "$ran1" . ';', $chrs[$ran1-48], $done);

    $done = str_replace('&#' . "$ran2" . ';', $chrs[$ran2-48], $done);

    $done = str_replace('&#' . "$ran3" . ';', $chrs[$ran3-48], $done);



    return $done;

}





// RETURN AN OBFUSCATED EMAIL LINK

function eMail($email='feedback', $subject='', $label='', $title='Email Link', $class='mailto')

{

    // SET DEFAULT VALUES FOR DOMAIN, EMAIL, SUBJECT AND LABEL

    $domain = $_SERVER["HTTP_HOST"];

    if (preg_match('#^www\.#i', $domain))

    {

        $domain = substr($domain,4);

    }

    if (!preg_match("#@#", $email))

    {

        $email .= "@" . $domain;

    }

    if (empty($subject))

    {

        $subject = "?subject=$domain Inquiry";

    }

    else

    {

        $subject = "?subject=$subject";

    }

    if (empty($label))

    {

        $label = "$email";

    }



    // OBSCURE SOME THINGS, BUT NOT CSS CLASS

    $mailto  = obscure('mailto:');

    $email   = obscure($email);

    $subject = obscure($subject);

    $label   = obscure($label);

    $title   = obscure($title);

    $string

    = '<span style="white-space:nowrap;">'

    . '<a class="'

    . $class

    . '" title="'

    . $title

    . '" href="'

    . $mailto

    . $email

    . $subject

    . '">'

    . $label

    . '</a>'

    . '</span>'

    ;

    return $string;

}





// CAPTURE THE ENCODED STRINGS

ob_start();





// CHARACTER CHECK USING THE OBSCURE CLASS STANDALONE

$str = 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789';

echo PHP_EOL . '<br/>' . $str;

$new = obscure($str);

echo PHP_EOL . '<br/>' . $new;





// DEMONSTRATE THE EMAIL TEST CASES

echo PHP_EOL . '<br/>' . email();

echo PHP_EOL . '<br/>' . email('RAY');

echo PHP_EOL . '<br/>' . email('Ray.Paseur@Gmail.com');

echo PHP_EOL . '<br/>' . email('foo', 'Your New Subject Here');

echo PHP_EOL . '<br/>' . email('Ray.Paseur@Gmail.com', 'Whats Updock?', 'Click to Email', 'Click This to Send Email', 'my_style');





// SHOW THE EFFECTS OF RANDOM REPLACEMENTS

$new = ob_get_contents();

echo PHP_EOL . '<br/>' . "STRLEN: " . number_format(strlen($new));

Open in new window

0
 

Author Closing Comment

by:Solenthaler
Comment Utility
Thank you very much - i will adopt it so that it can handle e-Mail and mailto links aswell.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now