-4

I would need to gather a list of all the user agents of the visitors of my website. Is that possible?

I essentially need a log like this:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36 
Mozilla/5.0 (X11; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0 
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36

I tried with this:

<?php
$file = 'user_agents.txt';
$browser = $_SERVER['HTTP_USER_AGENT'] . "<br>";
file_put_contents($file, $browser, FILE_APPEND | LOCK_EX);
?>

To test it I made a .php file with this code inside and I uploaded it on my server. Then I opened it via browser but it didn't create a .txt file in the same folder. What should I do to make it work?

Thanks in advance

La faguette
  • 541
  • 4
  • 11
  • 2
    What server are you running, there usually is an option to log that information at the server level. 1. https://www.iis.net/configreference/system.webserver/httplogging 2. http://httpd.apache.org/docs/1.3/logs.html#accesslog If you wanted to do this with your own script it would be better to use a DB than text files. It sounds like your current script isn't being executed as PHP. Is its extension `.php` and do you have PHP running? – chris85 Sep 29 '15 at 19:26
  • 2
    PHP isn't being parsed by your webserver. – HPierce Sep 29 '15 at 19:28
  • 3
    possible duplicate of [PHP code is not being executed (I can see it on source code of page)](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) – HPierce Sep 29 '15 at 19:30
  • not even putting this code in a php file on my server where the website (written in php) is hosted solves works. I tried to open it but the .txt file is not being generated on the server. – La faguette Sep 29 '15 at 21:28
  • so basically this (http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) is not helping me. Sorry for the desktop thing, that was stupid. :o – La faguette Sep 29 '15 at 21:30

2 Answers2

0

@HPierce was right, the problem was just that my webserver wasn't running PHP.

La faguette
  • 541
  • 4
  • 11
-1

PHP is server side script language that runs on the apache http server. It does not run in your browser. Giving a file the php extension on your desktop and opening with your browser does nothing.

Julio Soares
  • 1,200
  • 8
  • 11