Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a Simple Link Counter

Status
Not open for further replies.

AcidReignX

Programmer
Feb 28, 2005
32
US
I've been trying to figure out how to keep a very basic link counter through javascript. At first I didn't think it was possible with javascript because of it's inabilities with databases. Even so, does it have to rely on perl or something?

I've looked through a couple scripts on the internet to try and figure out how they work, but they don't actually tell you how it's working, they just give you the script. Thats great and all, but I'd like to have an explanation of HOW this stuff is working so that if I want to reproduce it (which I will), I will be capable of doing so.

So can somebody teach me how to log the number of times a link has been clicked? Preferably from an external script.
 
At some point you are going to HAVE to have some server-side code running. There is no way for the client to keep this information, since it knows nothing about others who have clicked the same links. Usually the way this is done it to call a server-side program and pass it the URL to actually go to. Once the program looks up that url and increments its counter it does a redirect to the url.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I'm perfectly fine with using server-side code, but I can't imagine how javascript can run such a script, for example, php. I don't want any redirects or anything like that.

If I can run a php script from an external .js file (which to my knowledge is impossible unless you open a popup... which I won't do) I am fine with that.

Simply from having had this response I've been able to sort out a little bit in my head as to how this all works though, so thank you n_n
 
You can incorporate php and javascript like this:

count.html
Code:
<html>
<head>
  <title>Counter Test</title>
<style type="text/css">
.count { width: 50px;
         font_family: verdana;
         font-size: 24px;
         color: #00FF40;
         background: #000000;
         text-align: center;
       }
</style>  
<script type="text/javascript" src="phpCount.php"></script>  
</head>
<body>

<div class="count">
<script type="text/javascript">
document.write(phpCount_txt);
</script>
</div>
</body>
</html>

phpCount.php
Code:
<?php
$file = "count.txt";
// open the file to both read and write
$handle = fopen($file, 'r+');
// take off the newline with trim()
$hits = trim(fread($handle, filesize ($file)));
// put our pointer back at the beginning
rewind($handle);
// write our new count
fwrite($handle, ++$hits);
// close the file
fclose($handle);

// send the info to the page via js variable
echo "var phpCount_txt = $hits;";

?>

count.txt
Code:
22

This is just an example.

You might visit PSC and check out the php scripts there.

Thanks,
--Mark
 
I think you need to clarify whether you want a "page hit counter" or a "link click-thru counter". They are two different animals with different implementations (the code above is the former).

A page hit counter will count how many times a particular page on your site has been viewed. If you use it on more than one page, the code above will not work. You need to use a different file name for each page, or use a database with a different key for each page.

A link click-thru counter will count how many times people have clicked on a particular link on your site to go to another page or even another site. To implement this you have to call your counter program on the server for each link you want tracked, and pass the url for the actual page you want to link to. Your program will use the passed url as a key (usually to a database file) and increment the counter for that key, then use a redirect to send the person to the actual destination url.

Hope that helps.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top