How do I know where they came from visitors

Hanging on a website counter, I suddenly found that not one of the organizations that provide them to me, does not allow to see which query in Yandex (and other search engines) that a man came to my site. At least, no one offers this service free of charge.

Then I was offended by them for the fact that I, in this case, will have to write yourself a little logger. The task - the easiest, because and scrap to do it. However, the owners of any site-counters have the audacity to take the money for it, instilling in people the belief that their money is not in vain shoulder. Well, dispel their hopes to fuck us!

Page, which came

So, to get the page from which the user came to this - a breeze. Its default address is passed in the HTTP header of the user-query string "Referrer:". Get its value from PHP by calling the corresponding function as follows:

  getenv ( "HTTP_REFERER") 

So, basically, you can just go and write a separate function that:

  $ H = getenv ( "HTTP_REFERER");  // Receives a URL, from which a visitor came 
 $ F = fopen ( "mylog.log", 'a');  // Opens a file with logs to add 
 flock ($ f, 2);  // Disables access to it until 
 // Until it is closed (in case two 

 // Script at the same time want to write something 
 // In the file, one of them will have to wait) 
 fwrite ($ f, "$ H \ n");  // Writes to a file received three stitches above URL 
 fclose ($ f);  // Closes the file 

Then this function can be called at the beginning of kakzhdogo php-script pages of the site.

How to find ip, their fate and exact time

But! Since we're here, we can finish a little function to find the visitor's IP (as it is possible to roughly estimate the area where it is physically located), his browser (interestingly) and a page where he actually came (sounds a bit strange - we train and so we know that he has come to our website, but imagine that you have this function is called in the header section of code that is inserted in the same way all of your pages - and so, perhaps, you did). Also it would be nice to have the time, when people come to our site - and then it will be possible to analyze user activity, etc.

So, the function takes the following form:

  $ Er_time = date ( "H: i: sd M Y");  // Write the current time in a row, 
 // Using the specified format 
 $ U = getenv ( "HTTP_USER_AGENT");  // Get the data about the software, 

 // Which ispol'uet user 
 $ H = getenv ( "HTTP_REFERER");  // Get the URL, from which a visitor came 
 $ R = getenv ( "REMOTE_ADDR");  // Get the visitor's IP 
 $ W = getenv ( "REQUEST_URI");  // Get the relative address of a page, 
 // Which requested the visitor 
 $ F = fopen ( "logs / visits.log", 'a');  // Further - it is clear, we write all this to a file 
 flock ($ f, 2); 
 fwrite ($ f, "$ er_time \ n Br: $ U \ n Rf: $ H \ n IP: $ R \ n Rq: $ W \ n"); 
 fclose ($ f); 

But this function is far from perfect! The fact that the Russian character you in these urlah not see - they will replace them with 16-hexadecimal representation (preceded by "%" sign). So be nice to your own script led them to a readable form: replaced all sorts

  http://www.yandex.ru/yandsearch?text=%E9%EE%E6%FB%E3+%F4%F2%F3%EC%E0%ED%E5&stype=www 

on

  http://www.yandex.ru/yandsearch?text=йожыг+фтумане&stype=www 

To do this, we will be the next piece of code using regular expressions:

  while (ereg ( '% ([0-9A-F] {2})', $ H)) {// while in line $ H is at least one 
 // Combination sign% and the two characters from the ranges 0-9 and AF 
 // (Hexadecimal digits) 
 $ Val = ereg_replace ( '*% ([0-9A-F] {2}) *..', '\ 1', $ H); 
 // Assign $ val result of the replacement of expression, 
 // Contains the percentage and two symbols of the intervals above the aforesaid, 
 // On these same two characters 
 // Short, now we have $ val next 2 characters, 
 // In front of which stood a percentage of the original terms 
 $ Newval = chr (hexdec ($ val));  // Get the charset value number, 
 // Get translated resulting in $ val 
 // Hexadecimal number in the "normal" 
 $ H = str_replace ( '%' $ val, $ newval, $ H.); 
 // Standard string replacement function - 
 // Replaces a substring consisting of the interest and druh characters 
 // Of the variable $ var on a symbol that these two 
 // Hexadecimal characters coded 
 } // End of cycle :)

Total:

  $ Er_time = date ( "H: i: sd M Y");  // Write the current time in a row, 
 // Using the specified format 
 $ U = getenv ( "HTTP_USER_AGENT");  // Get the data about the software, 
 // Which ispol'uet user 
 $ H = getenv ( "HTTP_REFERER");  // Get the URL, from which a visitor came 
 $ R = getenv ( "REMOTE_ADDR");  // Get the visitor's IP 
 $ W = getenv ( "REQUEST_URI");  // Get the relative address of a page, 
 // Which requested the visitor 
 while (ereg ( '% ([0-9A-F] {2})', $ H)) {// while in line $ H is at least one 
 // Combination sign% and the two characters from the ranges 0-9 and AF 
 // (Hexadecimal digits) 
 $ Val = ereg_replace ( '*% ([0-9A-F] {2}) *..', '\ 1', $ H); 
 // Assign $ val result of the replacement of expression, 
 // Contains the percentage and two symbols of the intervals above the aforesaid, 
 // On these same two characters 
 // Short, now we have $ val next 2 characters, 
 // In front of which stood a percentage of the original terms 
 $ Newval = chr (hexdec ($ val));  // Get the charset value number, 
 // Get translated resulting in $ val 
 // Hexadecimal number in the "normal" 
 $ H = str_replace ( '%' $ val, $ newval, $ H.); 
 // Standard string replacement function - 
 // Replaces a substring consisting of the interest and druh characters 
 // Of the variable $ var on a symbol that these two 
 // Hexadecimal characters coded 
 } // End of cycle :) $ F = fopen ( "logs / visits.log", 'a');  // Further - it is clear, we write all this to a file 
 flock ($ f, 2); 
 fwrite ($ f, "$ er_time \ n Br: $ U \ n Rf: $ H \ n IP: $ R \ n Rq: $ W \ n"); 
 fclose ($ f); 

Already on logs this script can be seen, where the visitor came, on what page he was held, and on what page of the site left. Yes, of course, you can do it and sessions - but we were too lazy. And so - two minutes, and you can enjoy! :)

Do you recognize it from the thousands

This - the basis. Then you can think of a lot of things: for example, rows that contain "http://www.yandex.ru/yandsearch", cut the part in which, in fact, contains a query and write to any file type "yandex.log ". In general, at that fancy enough - everything can be zababahat!