Log in

View Full Version : click counter when link is pressed



kimberlyhello
07-20-2008, 05:51 AM
Hi,

I would like to incorporate a counter that counts up every time a link is clicked.

I don't want to use a database...just one link that says "click here" and a counter displayed right next to the link that starts at 0 and counts up every time the link is clicked. I would also like the browser to be automatically reloaded after the link is clicked so that the user can see the number go up by one each time it is clicked.

Is this easy using php?

Any help would be appreciated. Maybe someone could just point me in the right direction, I don't even know where to start.

Thanks!

TheJoshMan
07-20-2008, 08:58 AM
I'm not real good with PHP, but I wrote a small javascript that will do what you're asking for...



<HTML>
<HEAD>
<TITLE> Clicker Counter Thingamajig </TITLE>
</head>

<body>


<script type="text/javascript">

var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}

document.write('<a href="#" onclick="linkClick()">Click Me!</a>');


</script>

You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.



</BODY>
</HTML>

Jesdisciple
07-20-2008, 07:08 PM
I would like to incorporate a counter that counts up every time a link is clicked.

[...]

Is this easy using php?For a beginner I would say, "Yes and no"; it's very simple, but the hard part is the one that actually records and retrieves the count. And it must be somehow recorded outside of PHP, because a PHP execution is both finite and one-for-one (i.e. one execution of PHP can only respond to one client request).


I don't want to use a database...If you don't want a database (a collection of huge files) then how about a simple file?


Any help would be appreciated. Maybe someone could just point me in the right direction, I don't even know where to start.Start with the Filesystem Functions (http://www.php.net/manual/en/ref.filesystem.php), particularly file_get_contents (http://www.php.net/manual/en/function.file-get-contents.php), file_put_contents (http://www.php.net/manual/en/function.file-put-contents.php), and file_exists (http://www.php.net/manual/en/function.file-exists.php).

And make sure to ask in this thread if you get stuck.

techietim
07-20-2008, 07:26 PM
<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>counter example</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
<a href="?click=yes">clickMe</a>
</body>
</html>

kimberlyhello
07-21-2008, 04:21 AM
Thanks Nyne Lyvez, Jesdisciple, and techietim. I really appreciate all of your help.

Nyne Lyvez - thanks for your javascript. I just wished that the numbers on the counter were permanent.

Jesdisciple - Yes, a simple file would be ok to use instead of a database. Sort of like what techietim posted here.


techietim - I tried using your script. First I made a txt file called counter.txt and then I uploaded the script that you posted. But it didn't work. And how would I get the numbers to be displayed on the same page?

I will try to use this script and just keep working on it. Thank you again.

techietim
07-21-2008, 11:57 AM
@kimberlyhello

You may have to CHMOD the counter.txt file to 666

kimberlyhello
07-22-2008, 01:09 AM
techietim - How do I do the CHMOD thing? How do I change it to 666?

thetestingsite
07-22-2008, 01:21 AM
When in your FTP client, you should be able to right click on the file and choose something along the lines of permissions or chmod or something to that effect.

Hope this helps.

kimberlyhello
07-22-2008, 02:07 AM
Thanks. I got the chmod to 666. But the script is still not working.

Any other ideas? Am I still doing something wrong?

I am using (techietim's script): I named it test.html and also have a text file named counter.txt in the same folder as test.html


<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>counter example</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
<a href="?click=yes">clickMe</a>
</body>
</html>
!

Just after writing the above post, I realized that test.html should be test.php. After I changed that, it worked perfectly!

Thanks to all who helped me. I am still learning and so I always appreciated the help.

One last question (which is very important for what I am trying to accomplish on my website) - How do I make it so that I can have multiple different unique links that all work the same way as this one. For example, if I have a link like this: http://www.mysite.com/profile.php?uid=30 but I also what to have all the other user id links as well (http://www.mysite.com/profile.php?uid=29 and http://www.mysite.com/profile.php?uid=27 and so on). But I want them to all count up independently. Can I just add "?click=yes" to the end of the link some how? help!

Thanks.

kimberlyhello
07-25-2008, 04:13 AM
techietim??? Are you still there?

Jesdisciple
07-25-2008, 04:18 AM
I can't figure out how ?click=yes would help with the independence, but I have two ideas for that.

Doesn't each profile have its own database entry? Can't you just put the profile's visit count in that entry?

Otherwise, the text file needs to store which user each number applies to. This could be done by either changing the text put in the file or using a different file for a different user.

motormichael12
07-25-2008, 04:24 AM
Save it as "test.php", not html

kimberlyhello
07-29-2008, 06:29 AM
Yes, each profile has its own mysql database entry. But I don't want to use the database for the link counter. I would rather just have a unique link for every person's profile. And then somehow make the number go up when the link is pressed. I almost have it done with the code that was posted earlier in this thread, but now I need it to work on many different profile pages. Not just one.

Thanks again for any help.

Jesdisciple
07-29-2008, 07:06 AM
I don't understand that, but anyway... You're going to be dependent on the database however you do it. I think the database would be the easiest route (for the server), but the second-easiest would be a modified filename. The filename will depend on the user's ID which will come from the DB.
<?php
$id = // the user's ID
$file = $id . '.txt';
if(!file_exists($file)){
file_put_contents($file, '0');
}
if($_GET['click'] == 'yes'){
file_put_contents($file, ((int) file_get_contents($file)) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>counter example</title>
</head>
<body>
<h1><?php echo file_get_contents($file); ?></h1>
<a href="?click=yes">clickMe</a>
</body>
</html>

WandaLL
10-22-2008, 11:59 PM
I believe this is just what I need but as a newbie, I have a dumb question. Where does the link reside that the user would click?

Thanks
WLL


I'm not real good with PHP, but I wrote a small javascript that will do what you're asking for...



<HTML>
<HEAD>
<TITLE> Clicker Counter Thingamajig </TITLE>
</head>

<body>


<script type="text/javascript">

var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}

document.write('<a href="#" onclick="linkClick()">Click Me!</a>');


</script>

You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.



</BODY>
</HTML>

TheJoshMan
10-23-2008, 12:04 AM
document.write('<a href="#" onclick="linkClick()">Click Me!</a>');


That is the link written by javascript... However, I would recommend using one of the version the other guys posted, as I am definitely not very good at scripting. I'm more of a css kinda guy.

Jesdisciple
10-23-2008, 12:24 AM
Does the click count need to span responses? If User A clicks the link and leaves the page, and User B does the same, should the count be 1 or 2? If User A follows the link twice but in two different loads of the page, should the count be 1 or 2?

If you answered 1 the first question, Nyne's script will do it. The link will appear in place of the script tag.

If you answered 2 to both questions, you need to use a simple database or other file, no cookies required. Both techietim's and my own script implement this.

If you answered 2 and 1, you need to use cookies to record either 0 or 1 (a Boolean) click for each user and add them together via a database.

WandaLL
10-23-2008, 02:20 AM
I believe small javascript function is just what I need but as a newbie, I have a dumb question. Where does the link reside that the user would click?

Thanks
WLL

TheJoshMan
10-23-2008, 02:27 AM
<HTML>
<HEAD>
<TITLE> Clicker Counter Thingamajig </TITLE>
</head>

<body>


<script type="text/javascript">

var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}

document.write('<a href="#" onclick="linkClick()">Click Me!</a>');


</script>

You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.



</BODY>
</HTML>

The highlighted section is the link

Jesdisciple
10-23-2008, 02:29 AM
Here's a simplified version of Nyne's script, where the link is not generated or changed by JavaScript; it will look exactly as below. If you still don't get something please specify what it is.
<HTML>
<HEAD>
<TITLE> Clicker Counter Thingamajig </TITLE>
<script type="text/javascript">
var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}
</script>
</head>

<body>


<a href="#" onclick="linkClick()">Click Me!</a>

You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.



</BODY>
</HTML>

sanjeetbisht123
02-07-2009, 08:43 AM
your page should be in php format to run this script
just make a page in html and save it in php format

ipbr21054
07-29-2010, 09:33 AM
Hello,
I am new here and would like some help if i could ask please.
I am using the script below which is a click counter to tell me how many peolple visit a site once the hypertext link is clicked on.
The problem i have with it is that it does not keep the recorded hits correctly.
If i click on the link 10 times then 10 is shown in the result box which is on the screen but as soon as i close the browser and then open it again the 10 hits that were shown in the box now show 0.

Could somebody edit the script so every time the page has been visited the results will update and stay recorded each time.
Thanks very much

<HTML>
<HEAD>
<TITLE> Clicker Counter Thingamajig </TITLE>
<script type="text/javascript">
var clicks = 0;
function linkClick(){
document.getElementById('clicked').value = ++clicks;
}
</script>
</head>

<body>


<a onclick="linkClick()" href="http://www.connells.co.uk/detail.asp?type=0&src=property&cs=88&bs=WOT&br=-1&prop=100003&min=150000&max=-1&bed=3&page=6&id=WOT302735">CLICK HERE</a>

clicked <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.



</BODY>
</HTML>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-gb">
<title>About Me</title>
</head>

<body bgcolor="#FFFFFF">
<p align="center">
&nbsp;</p>


<div align="center">


&nbsp;</div>

</body>

</html

fileserverdirect
07-30-2010, 04:25 AM
Thats because it's client side, If you want javascript to remember it, you can use cookies; there are PLENTY of tutorials on the internet for that.

covuk77
08-24-2010, 11:18 PM
Hi there, I stumbled across this forum whilst trying to find a way of craeting a click counter.

This may seem like a daft question, but is there a way of having another link that would decrease the number displayed?

Is it also possible to create a link that would plus/minus by larger increments (i.e.) ten?

I am very new to all this and would very much appreciate the help!

caomhan
01-26-2011, 02:19 PM
Hey there!

I have developed an download and click counter, too!
My counter is in the new and modern facebook style but can be styled as you wish.

Maybe you have to translate the page via the google translator.

http://www.ka-mediendesign.de/blog/click-und-download-counter-fuer-links/

Example are in this article, too.
This counter were programmed in javascript an php.
When everything is running you just have to give the link the class "downloadCounter" an the counter appears and counts! :)

I hope you understand everything.

Best regards,
caom

mmoreno
05-12-2011, 05:10 PM
Not sure if anyone can still help but I am looking for a click counter that keeps track of the clicks made even after navigating to another page. I do not want it to restart at 0. Please let me know if you can help, my job depends on it! :-/

http://www.ka-mediendesign.de/blog/click-und-download-counter-fuer-links/

Barry McMahon
05-02-2012, 10:53 PM
Hi, I have everything working according to the steps i have read here in order to create two buttons (Rollover in Dreamweaver HTML) which increment the counter by 1. Unfortunately, they increment each others counters despite the fact that I have two distinct php files, named differently and two txt files also named differently from one another. i am unsure what code to supply but here is the rollover code...

<a href="Vote.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('VoteObama','','images/ObamaSelected.png',1)"><img src="images/ObamaVote.png" alt="Vote for Obama" name="VoteObama" width="280" height="280" border="0"><?php include 'VoteCounter.php';file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1); ?></a>

<a href="Vote.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('VoteRomney','','images/RomneySelected.png',1)"><img src="images/RomneyVote.png" alt="Vote for Romney" name="VoteRomney" width="280" height="280" border="0"><?php include 'VoteCounterRomney.php';file_put_contents('counterRomney.txt', ((int) file_get_contents('counterRomney.txt')) + 1); ?></a>

Any help is greatly appreciated!