View Full Version : Link hit counter

01-02-2012, 05:13 PM
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.


// 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:

<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?

Old Pedant
01-02-2012, 09:40 PM
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.

01-03-2012, 06:49 AM
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.

Old Pedant
01-03-2012, 08:26 AM
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.)

01-03-2012, 08:30 AM
OK so cookies are out.
Is my only option is PHP
Any suggestion?

Old Pedant
01-03-2012, 08:06 PM
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???

01-03-2012, 08:31 PM

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.

Old Pedant
01-03-2012, 08:49 PM
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.

01-03-2012, 08:59 PM
Thanks a mil wil try the PHP.