Jump to content

Link Click Counting

- - - - -

  • Please log in to reply
16 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Id like to create a script that shows how many times a link has been clicked on my site

Is there a way to do this via PHP/MySQL?

Any tutorials/examples??

#2
Smilex

Smilex

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
make a javascript or php function that is executed by the onclick attribute that you put in your <a> tag.
PHP is good for if you want to store this count in a database
Javascript is if you want to work on the client computer
JavaScript and HTML DOM Reference <- that is w3schools's section on Javascript

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Yes, an javascript is one option. the other option is to use what for example facebook uses, a middlepage that takes the url as a parameter and then makes a header("Location") to let the user be directed to the correct url, while the middlepage itself does the statistics.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Any example code I could use for this?

I'm not THAT experienced with PHP as I am still learning.

So what you're saying is, the Link would be pushed to the middle page, where it would then carry out a function to count that statistics, and then direct them to the actual page?

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Yes, more or less. Usually, you prefixes the actual link with your middelpage so you write:
<a href="l.php?url=http://www.microsoft.com">Microsoft</a>
in your HTML. this calls l.php (facebook calls this l.php, so I used them as example. you can call it whatever you like of course) with the actual url as a parameter.
in your code then, you do something like this:

<?php

$link = $_GET['url'];

header("Location: $link");

// here you do your database stuff so you count the link click 

// for example, you can store referring url, so you know what page they clicked the link on and whatever you like.

?>


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
I see, and then in order to count the visits I could use a simple MySQL insert and +1 to whatever number was in the field in the SQL table to begin with?

I need the links to be friendly to Spiders/Crawlers, would using this kind of URL still be able to create all important Backlinks to sites or would it make the link unfriendly to spiders and search engines etc?

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
the link would not be so friendly I guess, but that's not within my knowledge area. an OnClick event on the link throwing an ajax request to a php script would probably be better for Spiders...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Thinking about it I doubt there would be any easy way to make it spider friendly.

Unless it would be possible to remove the l.php? via a htaccess MOD_REWRITE function?

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
you could of course make a subdir /link/http://... instead but you still have a call to your server first which can make trouble in seo
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Yeah, I think there's proably going to be an issue with this completly, I'm sure there would be a workaround.

Thank's for your input!

#11
Smilex

Smilex

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
Plain Text code - 11 lines - codepad
I really don't see why you would link like explained in this thread, other than doing the example I posted above

#12
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
That was looks interesting, is it Javascript or PHP, is the OnClick event a.

Does OnClick within the Hyperlink require any Javascript/PHP Tag Openers/Wrapping or would that just work 'Out-the-Box'




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users