fergal — 2013-02-18T05:14:05-05:00 — #1
I'd like to put a link on a webpage and show a count of the number of clicks that link has received.
The link might look like this:
How do I do that?
I'm slowly teaching myself a little PHP, so I'm not looking for the full code. Some pointers on how to do it would be great?
pullo — 2013-02-18T07:25:28-05:00 — #2
Would the link be to an external site or a separate page with your site?
fergal — 2013-02-18T07:26:53-05:00 — #3
Thanks Pullo, it would link to an external site. I won't be able to add any code to the site it links to.
pullo — 2013-02-18T07:31:50-05:00 — #4
When the link is clicked, this event listener could then fire off a quick AJAX request to a PHP script that increments your link click count by one (either in a txt file or database).
Following this, you could then direct your visitor to their intended goal.
jimmybrion — 2013-02-18T07:34:55-05:00 — #5
I think there are 2 solutions to it.
You can save the click count in database and increment it with a small request when the link is clicked and before redirecting it to the external site.
Upload a txt file on server and update it with the count, when the link is clicked.
fergal — 2013-02-18T07:50:02-05:00 — #6
Thanks again, if I can figure out how to store the number of clicks in a variable, I'm comfortable with outputting that number on a HTML page.
Thanks Jimmy. With no. 2.
"Upload a txt file on server and update it with the count"
I know how to do that.
"when the link is clicked"
How would my script know when the link is clicked? To put it another way, if I put the code to update the count in a text file inside an if statement. What condition should I use for the if statement?
Assuming the link gets a lot of clicks, would using a text file like that be an efficient way to do it, or would it slow down my site?
pullo — 2013-02-18T07:53:57-05:00 — #7
Nah, probably not.
If you get to the point where you have enough visitors to slow your site by clicking on this link, whether to use a text file or not will be the least of your worries.
Just thinking out loud, you could code the link like this:
And have linkcounter.php update the count, grab your destination from the query string, then set the header to redirect you to your original goal.
2ndmouse — 2013-02-18T08:04:18-05:00 — #8
If you want something that's relatively easy to set up and monitors clicks and downloads - try 'Click Counter'
I've been using it to count downloads for a few months and it appears to be reliable. A free version is available.
jimmybrion — 2013-02-19T07:23:04-05:00 — #9
Yes, I will also recommend the same.
fergal — 2013-02-19T10:18:17-05:00 — #10
Thanks, will try to do it that way.
Thanks for the suggestion, looks like it might be an option.
jaismin_madona — 2013-02-20T03:43:02-05:00 — #11
There are two ways to do this one save the click count in database and increment it with a small request when the link is clicked and before redirecting it to the external site and second is that Upload a txt file on server and update it with the count, when the link is clicked.