View Full Version : Using javascript in a php document

Retired Bill
11-16-2006, 04:53 PM
I have exhausted my library of PHP and JAVASCRIPT books and can still not get my code to do what I need it to do. I am trying to hide an email link from spiders so that I will not be as vulnerable to spammers. The link is in a menu which is in table form. Can someone spot where I've gone wrong? This is the function I defined. It is in a document named mail.js:

function HideEmail()
var string1 = "foo";
var string2 = "@";
var string3 = "bar.biz";
var string4 = "Contact Us";
document.write("<a href=" + "mail" + "to:" + string1 +
string2 + string3 + ">" + string4 + "</a>");

I placed this command in the heading of the page. This one of two external js files used on my page:

<script src="mail.js" language="JavaScript" type="text/javascript"></script>

This is where I call the function in the document:

<php? print "<td width=\"20%\"><div align=\"center\" class=\"style1\"><script language=\"javascript\" type= \"text/javascript\">HideEmail();</script>"; ?>

This is what I see when I view the page in the browser:

"; ?>

11-16-2006, 06:06 PM
look very closely at your opening PHP tag

11-16-2006, 06:22 PM
I developed a PHP function to do this already:


function SecureEmail($email,$subject=''){

$myRand = rand(0000,9999);

for ($count=0;$count<strlen($email);$count++) { // convert email to ascii values
$ascEmail .= ord(substr($email,$count,1));
if ($count < strlen($email)-1) { $ascEmail .= ','; }

// We start building the Secure Link in Javascript
$link = '<script type="text/javascript">';
$link.= chr(10)."/********************************************************************".chr(10)."* SecureEmail.php - Protect mailto links from harvester bots".chr(10)."* Developed by Shane Kretzmann at www.WalkerSystemsSupport.com".chr(10)."* *******************************************************************/".chr(10);
$link.= chr(10).chr(9).'var emailarray'.$myRand.'= new Array(';
$link.= $ascEmail.')'.chr(10).chr(9);
$link.= "var postemail$myRand=''".chr(10).chr(9);
$link.= 'for (i=0;i<emailarray'.$myRand.'.length;i++)'.chr(10).chr(9).chr(9);
$link.= 'postemail'.$myRand.'+=String.fromCharCode(emailarray'.$myRand.'[i])'.chr(10).chr(9);
$link.= 'document.write(\'<a href="mailto:\'+postemail'.$myRand.'+\'?subject='.$subject.'">\'+postemail'.$myRand.'+\'</a>\')'.chr(10);
$link.= '</script>';

return $link; // Echo the code that will produce the secure email link in Javascript Code



echo SecureEmail('Me@MyDomain.com');
echo SecureEmail('Me@MyDomain.com','Optional Email Subject Here');


<script type="text/javascript">
* SecureEmail.php - Protect mailto links from harvester bots
* Developed by Shane Kretzmann at www.WalkerSystemsSupport.com
* *******************************************************************/

var emailarray5280= new Array(77,101,64,77,121,68,111,109,97,105,110,46,99,111,109)
var postemail5280=''
for (i=0;i<emailarray5280.length;i++)
document.write('<a href="mailto:'+postemail5280+'?subject=">'+postemail5280+'</a>')



Retired Bill
11-16-2006, 06:47 PM
Thanks for the second pair of eyes. Mine don't seem to be working very well. Maybe it's my new glasses.