Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Web Question - logging links

Options
  • 04-02-2008 4:53pm
    #1
    Registered Users Posts: 500 ✭✭✭


    Hi folks,

    Have a question.

    Lets say i have a page with 10 external links on it. I want to log when users click these links.

    What options do i have?

    I can use html and javascript(AJAX) for this.

    Please help


Comments

  • Closed Accounts Posts: 81 ✭✭dzy


    Yep. I think AJAX might be a good way to go about this.

    I would have a database table to store each link with fields Id, Url and Counter.

    I would then have some Javascript function that would handle a link clicking event by submitting a request for a script on the server to increment the Counter field of the link. The Id of the link could be passed in the query string of the request.


  • Closed Accounts Posts: 81 ✭✭dzy


    Here is some code that will do it for you.

    Firstly, here is your html page :
    <html>
    <head>
    <script type="text/javascript">
    
        function incrementCounter(id)
        {
            var xmlHttp;
    
            try
            {
                xmlHttp=new XMLHttpRequest();
            }
            catch (e)
            {
                try
                {
                    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {
                    try
                    {
                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e)
                    {
                        alert("Your browser does not support AJAX!");
                        return false;
                    }
                }
            }
      
            xmlHttp.open("GET", "increment_counter.php?id=" + id, false);
            xmlHttp.send(null);
        }
    
    </script>
    </head>
    <body>
    <ul>
        <li><a href="#" onClick="incrementCounter(1)">Link 1</a></li>
        <li><a href="#" onClick="incrementCounter(2)">Link 2</a></li>
    </ul>
    </body>
    </html>
    

    And here is some PHP code showing you what the server side script might look like :
    <?php
    
        if (!isset($_GET['id']))
        {
            exit;
        }
    
        increment_counter($_GET['id']);
    
        function increment_counter($id)
        {
            // Here goes your code to increment the counter field
        }
    
    ?>
    


  • Registered Users Posts: 500 ✭✭✭warrenaldo


    Thanks DZy - ye i think this will be the way to do it. Thanks for the help


  • Registered Users Posts: 2,593 ✭✭✭tommycahir


    you could also have a look at something like awstats or webalizer they are both statistics programs that run on the webserver that provide information such as the top links clicked etc.

    They might be overkill for what your looking for but worth investigating anyways. :D


  • Moderators, Category Moderators, Motoring & Transport Moderators Posts: 21,238 CMod ✭✭✭✭Eoin


    dzy wrote: »
    Here is some code that will do it for you.

    Will that code redirect the user afterwards, or should this be added also?


  • Advertisement
  • Closed Accounts Posts: 81 ✭✭dzy


    eoin_s wrote: »
    Will that code redirect the user afterwards, or should this be added also?

    For me the browser follows the link. Although I've only checked it in Firefox.


Advertisement