Search code examples
javascriptfunctiononclickflat-file

javascript click tracker, ahref links


i have a script that produces dynamic links for swf files....

eg the script will produce

<a href=" { url } / swf / file . swf ">Click File</a>

when they click the link it opens the swf file in a lightbox so i cannot use php, as its client side

i can change script so that it will add

onClick="javascript: FUNCTION;"

how would i word a javascript function to write to a file in base directory named

'click-log.txt' with href of link and timestamp...

lets say the link clicked has the url

http://example.com/data/swf/file1.swf

i would like the logfile written as

1329849120 , 82.**.***.*** , /data/swf/file1.swf

i presume its easier to write the full file path, but i would be happy with just the filename, or even the full url, if it is easiest....

i have coded this to work with php on page load many times, but cannot write javascript to do this action.....

thanks guys


Solution

  • this goes in head (ajax request)

    <script type="text/javascript">
    function clickLog(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","click-log.php?url="+str,true);
    xmlhttp.send();
    }
    </script>
    

    this opens 'click-log.php' with url parameter 'str' <<< 'str' is defined as 'this.href' in the onclick function brackets

    <a href=" { url } / file .swf " onClick="clickLog(this.href)">Click Me</a>
    

    when the link is clicked it opens and processes the php file, with the link href (this.href) as the parameter url=

    the script even came with this.....

    <div id="txtHint"></div>
    

    if you place this div below the link to be clicked, it will echo whatever the click-log.php outputs......

    this will be explained below....

    this is my php file

    <?php
    
    $url = $_GET['url'];
    
    $time = date('U');
    
    $ip = $_SERVER['REMOTE_ADDR'];
    
    $fp = fopen('click-log.txt', 'a');
    $fwrite = fwrite($fp, $time.' , '.$ip.' , '.$url.'
    ');
    
    // --- echo 'Log Written'; --- // 
    
    ?>
    

    this writes to end of text file 'click-log.txt' with the timestamp, ip, and href of link clicked

    the echo line that is commented out, will insert the text "Log Written" into the "txtHint" div once the link has been clicked and the ajax request processed

    all files used are in the base directory however anyone who would like to implement this script on their site would probably already know how to change file locations etc

    ....

    ..... thanks for the info guys.... another successful script :)

    ps, now to write the script to show me the logfile in pretty graphs and pie charts :lmao: