There are two ways of doing this, the quick-and-dirty way, and the 'proper' way.
Qcuick and dirty may give faster responses, but the data you collect will only be quitable for the top-10 listing, nothing else.
The 'proper' will slow down slightly (milliseconds) as the table grows, but you can do regular maintenance to erase irrelevant data.
Quick and dirty:
The mysql table can be very simple, it only needs to hold the referer name and a 'hitcount'.
You will need to run at least two queries: one to find out wether the referer already exists, and one to update or insert the data.
The procudere is:
Query to see if the referer already exists in the database.
If yes, update the hitcount to hitcount+1
If no, insert the referer and set hitcount to 1
Advantages:
- takes very little space in the database
- is very fast the get the stats
Drawbacks:
- can lead to duplicate data in high-traffic environments
- generates very application-specific data
the Proper way:
Create a table that contains referer, remote-ip, date, and url.
With every visit, insert all this data into a new record.
You can get the stats by running queries like
SELECT referer, count(*) as hitcount
FROM table
GROUP BY referer
ORDER BY hitcount DESC
limit 10
Advantages:
- very detailed information, you can get a lot more data from it than just the top10
- Only one query required to insert new data
- No duplicate data in high-traffic sites.
Disadvantages:
- takes more space in the database
- will slow down a little as the table grows