Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using javascript in a php document

    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:

    "; ?>

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    look very closely at your opening PHP tag

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I developed a PHP function to do this already:

    Function:
    PHP Code:
    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


    Usage:
    PHP Code:
    echo SecureEmail('Me@MyDomain.com');
    echo 
    SecureEmail('Me@MyDomain.com','Optional Email Subject Here'); 
    Output:
    PHP Code:
    <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++)
            
    postemail5280+=String.fromCharCode(emailarray5280[i])
        
    document.write('<a href="mailto:'+postemail5280+'?subject=">'+postemail5280+'</a>')
    </script> 
    Enjoy!

    ~UberDragon

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the second pair of eyes. Mine don't seem to be working very well. Maybe it's my new glasses.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •