Skip Navigation LinksHome > Categories > Code from a Category

How do you know how many times have downloaded your file



User Name: serfcompany
Name: Serf
Contact Me: www.datawebcoder.com
Home Page: www.datawebcoder.com
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 4038
Add Date: 03/17/2012
Since then, as soon as your site is designed, first and most obvious is the desire to know how it is popular. There are different possibilities that can solve this problem.

Typically, a server that provides you with hosting, gives a very meager statistics. This statistic includes only the number of hits on the site and the amount of data transferred.
But you must admit that this does not even enough.
Files on this site very much and would like to have an idea of ??what they have downloaded.


So how can we solve this problem? There is a way - counters downloads. Do not worry, everything is very simple. You've probably already seen them.
Normally, this counter is set next to a link to a downloadable document.
Roughly, it looks like this: Link - Downloads [656]. 656 - this is a number that is incremented by 1 each time you download a file, a program that implements it - count downloads.


It is important to understand how it works the counter, and only then you can start writing code.


It is important to understand how it works the counter, and only then you can start writing code.


We have some site www.sayt. Let us assume that on this site need to count how many times download some books. References to books found in the file book.php. For convenience, create a separate directory Counters, which is very counter (counter.php) and text documents in which the data will be stored, how many times have downloaded the appropriate book (book1.txt and book2.txt).
You may ask, why not use it to store the number of downloads the database? Of course, book.txt can replace this base, but this requires special software. In addition, the text files is much easier to use than the database. And I hope your article is primarily for beginners.
Undoubtedly, a large online database use is a necessity. Without it, they can not do. Just the number of text files reaches this number, which will be administered to them almost impossible. But this is a topic for another article.


In the end, as a result, when we open the file book.php, you should see something like this:


Reference (Number of downloads: 25). 25 - a number that is incremented by one when you click on the link.


Now try to describe how this cycle works in general. First, load the page with our link and the inscription - "The number of downloads: 25". To take the number 25 should refer to the file where it is stored (book.txt). Once the user clicks on a link, the program starts (counter.php), which increases the variable by 1 and writes it to a text file. And so every time.


As you can see in theory is very simple. But the main problems arise in practice.


So, let's move on to the technical side of the issue. We start directly from the counter (counter.php). Here is the code that needs to be inserted into it.


?ounter.php


Well, as hard? Let us deal with.


First let's define a design code. As you know, to use PHP, you must create the following structure:



Thus, we give the program to understand that use PHP.


Since the number of downloaded files, to determine at what kind of link was clicked, use the construction:



etc. Thus, the selection is carried out using the variable $ book. The only way to make sure that when you click on a link that was appropriated by the corresponding variable?


When you want to go to a specific site, you need to type its URL.
For example:
www.sayt.ru / index.php
But in fact URL can have a longer look. Have you seen this record:
www.sayt.ru/index.php?parametry


This URL, as it were, divided into two parts: the first is the address to the site, and the second (after the sign "?") Specifies certain parameters. A separator is a question mark.


These parameters can be variables.


www.site.ru/index.php?variable = value


Pointing, as variables, they will be transferred to the opened file. In case you need to declare several variables, it is done through the sign "&".


www.site.ru/ index.php?variable 1= value& variable2 = value


Thus, when a user opens this page, variables are assigned automatically, which is very convenient. This method of passing variables into a script called by GET.


We apply this method in our case. Need to go into a document with links (book.php) and change the link to the file, as follows:


a href="Counters/counter.php?book=1">That is, first is a reference to the counter, and after the sign "?" specifies the name of the variable book, which then send the user to the appropriate file. Do the same for the second and third links.


a href="Counters/counter.php?book=2">
a href="Counters/counter.php?book=3">


That's the whole structure can now move on to the code of the counter.


Let us consider a code-counter.


- header("location: http://www.site./zip1.zip");- Actually this is a reference to a book, you want to download. header () - is a special function that can move to a new web-site, without asking the user. But to use this feature to be careful. Its main feature is that it can not run after the HTML code. In other words, it is impossible at first to put the html-code, and then, somewhere in the middle, header ().


- $file=fopen("book.txt","a+");


As mentioned above, in order to store a variable number of downloads, use a text file. Actually the whole problem lies in the counter to open the file, find out what value the counter now, to increase this value to 1 and close the file. That's it!


In PHP, to work with text files, be sure to perform the following steps:


1) Open the file
2) Extract data from a file and if necessary change them
3) Close the file


Special function to open the file: $ variable = fopen ("filename", "mode");


When you open the file you need to specify the mode that will allow to perform certain actions with the file after it opened. Modes that can be installed in the following table:


Mode
What happens if a file exists
What happens if the file does not exist


r
read-only
There will be a warning message

r +
Reading and Writing
There will be a warning message

w
write-only
This will create a new file. If the file exists, the new file will overwrite the old

w +
Reading and Writing
This will create a new file. If the file exists, the new file will overwrite the old

a
Adding data to the file
This will create a new file.

a +
Reading and adding data to the file
This will create a new file.

Thus, we open the file book.txt to read and modify its value. It creates the special variable $ file, which will be kept open file.


- flock($file,LOCK_EX);A very important element. It blocks access to the file at the time of the script. Imagine that both clicked on the link as soon as two people.
As the script opens the file for writing, it may fail, and just clear the counter.


- $ Count = fread ($ file, 100), then read the data from an open file $ file and assign it to variable $ count.


- $ Count + +; - increment by 1. This entry is equivalent to the following ($ count = $ count +1).


- Ftruncate ($ file, 0), - with this command, the file contents (book.txt) is cleared. And now you can easily record new information there. It is at this point may crash when running two simultaneous scripts. That's what I said above. But we have nothing to fear, the file is blocked.


- Fwrite ($ file, $ count);. Write the new value of the counter.


- Flock ($ file, LOCK_UN);. Now you can unlock the file


- Fclose ($ file);. Close.


When everything is written, be sure to set the attributes (right) on text files (txt). This can be done through the FTP-client. Change the file attributes to 666 (rw-rw-rw).


Well, that's all. I tried to understand as much as possible and just explain the process of installing the counter download.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.