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 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Question Link hit counter

    I have the following codes and it seems to work well but when exiting the site and re-entering it resets the values to 0 again.
    I am not sure how to proceed with this, been considering cookies but what if the user clears there cookies, I then looked at PHP but not understanding that to well at this stage.

    Javascript
    Code:
    // Function to count clicks on links
    var clicks1 = 0;
    var clicks2 = 0;
    var clicks3 = 0;
    var clicks4 = 0;
    
    function link1(){
        document.getElementById('clicked1').value = ++clicks1;
    }
    function link2(){
        document.getElementById('clicked2').value = ++clicks2;
    }
    function link3(){
        document.getElementById('clicked3').value = ++clicks3;
    }
    function link4(){
        document.getElementById('clicked4').value = ++clicks4;
    }
    HTML Code:
    Code:
    <tr valign="middle" align="center"><td colspan="1"><a target="_blank" href="http://mistiquestormelectronics.webs.com/" onclick="link1()"><img src="site_graphics/reinet.jpg" alt="Mistique Storm" width="120" height="90" /></a></td>
    <td colspan="2" align="left"><p>For all your Electronic equipment needs.</p></td>
    <td colspan="1" width="8%"><input id="clicked1" style="color: #000000; font-weight:bold; border-style: none; font-family: arial; background-color: #00FF00; text-align: center;" readonly="readonly" size="10" onfocus="this.blur();" value="0" ></td></tr>
    Any ideas, suggestions. Please Help?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    JavaScript variables are *NEVER* retained from one instantiation of a page to the next. No more so than are, for example, form field values.

    Yes, cookies or local storage (if permitted by the user) or server-side code (e.g., PHP or ASP or JSP or...) are the ways to retain "state." Server-side code is the safest, most likely to work in all cases, but even it (for most server-side systems) depends on the user allowing cookies or (in the case of ASP.NET, at least) passing along a session id in the querystring or in a hidden <form> field.

    There's no guaranteed to always work way, that I know of, if the user doesn't want to allow it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hans_cellc (01-03-2012)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks for your input, how would I then record this in a server side cookie. I know realize that it still needs to be allowed however being server side wil not be reset to zero.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Sorry, why do you want to use a cookie for server-side code.

    Oh, there are times when doing so makes sense, but I don't see the need for that here.

    I think an ordinary client-side cookie, using JavaScript is all you need.

    (Actually, cookies are *ALWAYS* client-side. It's just that server-side languages such as PHP/ASP/JSP allow the server to see the client-side cookies as an extra added feature.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    OK so cookies are out.
    Is my only option is PHP
    Any suggestion?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Explain to me what the purpose of this is.

    You want the clicks counted so you get a total for *all* users? Or do you want the counts on a per-user basis? Or???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Hi,

    I am wanting to give the friends who's links they are how many hits a link receives from this specific website showing on this site how many times the link has been clicked by all users.

    a sample of the page.
    http://www.sayorkies.co.za/friends.html
    Attached Thumbnails Attached Thumbnails Link hit counter-new-2.jpg  

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Then you have no choice but to use server-side code.

    ANYTHING you do in the browser can *ONLY* be seen by the ONE browser. You can't share information from one browser to another (even on the same machine! you can't share from MSIE to FF, for example...but never between machines in any case).

    So you *must* do this with server-side code: PHP or JSP or ASP. Choose your server-side platform and then post in the forum for that platform.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hans_cellc (01-03-2012)

  • #9
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks a mil wil try the PHP.


  •  

    Posting Permissions

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