Tracking Traffic to a Specific Link
My very first PHP script was dedicated to logging how many times a specific link has been clicked. The first version of the script the stats were logged to a flat file (text file located on the server) to avoid the need of MySQL. The LinkScript code will not immediately be available, but I will post it here in the future. Let's start with the history of LinkScript.
History of LinkScript
The first LinkScript was developed as a tool to count how many people have clicked on a specific link. The simplicity of this initial script allowed for all the data to be stored in a flat file on the server. This eliminated the need of MySQL for those uses that did not have access to a MySQL database on their server. The initial script would open the file, grab the line that is being accessed parse the data update the counter and than write all the lines back into the file with the newly updated count. This strategy shows a few problems, namely was errors would often be thrown due to write access and sometimes data would be over written.
At this point I had learned MySQL in and out and was able to tie LinkScript into a MySQL database. Although the original version of parsing a text file is still accessible, the flat file is not ideal. Incorporating a MySQL database allowed room for a lot of expansion. The expansions included, anti-hotlink options, extra statistics, including referrers and IP addresses of users clicking the link. The script was fairly simple, whenever there was a link you wanted to track you simply added the link to the database, and than the database would provide you with a URL to use to track the link. On that note let's get into the logistics of LinkScript.
Logistics of LinkScript
Tracking links can prove to be a difficult task, but one well worth it if you want to find out how popular a link is. The steps for the LinkScript would be as follows; add a link to the database, including name, description and the actual link URL. From this information the script would store that data in the database and return you the URL to use to track the link. This URL originally was in the form of linkScript.php?linkID=3. This may be changed for ease of use by adding a track back name so instead of linkID=3 it would be link=tack-back-name. Now when a user clicked on that link they would be re-directed to the file and or link that was requested. If the link indeed was a file, the PHP script knew that and if the anti-hotlink option was on for that link it would check the referrer and verify that the user came from the website. If they did not come from the website, or it looked like headers were modified they would be denied the file.
Future Revision to LinkScript
I have been working on future revisions for the LinkScript which include optimizing the database and the code and incorporate Object Oriented Programming into the script. Also a factor that may be looked into is making the actual links SEO friendly and figuring out if writing that would be worth the effort. In essence the URL would from linkScript.php?linkID=3 to /link-script/file-track-back-name.ext I am not sure if that would prove to be worth the effort but is definitely something to look into for the next revision.
As of right now the code is not available as I have not worked on this script in a few years. I am currently looking into that code and doing many revisions and some optimization of the script and once that has been completed expect to see the script available under the resources section.
Denver CO Blog
Tracking Traffic to a Specific Link