Joke Collection Website - Public benefit messages - How to count the number of times hyperlinks are clicked with php+mysql?

How to count the number of times hyperlinks are clicked with php+mysql?

There are two situations, one is the statistics before the page, and the statistics are sent when clicking, which is applicable to whether this link is an advertising link or other links. The second is to add statistics when opening the page, which is suitable for SEO or other needs, and count the click rate of the current page, regardless of the influence of the previous page.

Add a hook to the link. take for example

& lta href = " page link " onclick = " count hit(this)" & gt。 Link text

Then define a js function.

Function countHit(obj){

? var url = $(obj)。 attr(' href ');

? //I use jq here, based on links. Of course, for advertising, you can use

? //Advertising id is the statistical standard.

? $.ajax({

? Url: "Your php page address statistics";

? Type:' post',//Use post to prevent the browser from entering this address directly and being swiped.

? Data: {link:url}// Pass the link address post.

});

}

On the php page

$ link = add slashes($ _ GET[' link ']); //The link can be stored normally under security processing.

//Next, make a validity judgment, which is omitted here.

$ query = MySQL _ query ("select1from `statistical table` where` link` =' $ link'");

$ has = MySQL _ fetch _ array($ query);

If ($has){

//Current existence statistics

Mysql_query("UPDATE ` statistical table ` set hits = hits+1where` link` =' $ link' ");

} Otherwise {

MySQL _ query(" INSERT INTO ` statistical table `( link,hits) value ('$ link ',' 1 ')");

}

This completes the statistical content.

Current page statistics

Generally, this kind of statistics is the statistics of a certain information content. I assume that it is the statistics of a certain news, and there is a hits field in this information as statistics.

Just add it to the php page.

MySQL _ query(" UPDATE ` news table ` set hits = hits+ 1 where ` id `= ` news id ' ");

This sentence can complete statistics.