PHP File to Log Visitors

illmill

Registered Member
Joined
Jun 10, 2009
Messages
69
Reaction score
36
Hi guys, I basically want to upload a PHP script to my server that will give me this info for every single person/bot that lands on that page:
user agent, IP address, and referrer.

Any suggestions??
 
This should get you started at least -

Code:
<?php
$myFile = "testFile.txt";
$ip = $_SERVER['REMOTE_ADDR'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ref = $_SERVER['HTTP_REFERER'];
$stringData = $agent . " - " . $ip . " - " . $ref . "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
?>

Output will look something like this -

Code:
Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 - 174.125.36.177 -

Note that the $ref variable will be empty if you go to the page directly, so there should be some information behind the last "-".

HTH
ND
 
This should get you started at least -

Code:
<?php
$myFile = "testFile.txt";
$ip = $_SERVER['REMOTE_ADDR'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ref = $_SERVER['HTTP_REFERER'];
$stringData = $agent . " - " . $ip . " - " . $ref . "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
?>

Output will look something like this -

Code:
Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 - 174.125.36.177 -

Note that the $ref variable will be empty if you go to the page directly, so there should be some information behind the last "-".

HTH
ND

thats a good and simple solution, however i would advice you to use file_put_contents() and place a LOCK on the file. Depending on the site traffic it might loose some writes in your solution if there are much simultaneous visitors..
 
I would advise using a database (SQL) instead of a text file. Just slightly modify the code given above. Beware this file can get pretty big if your website gets a lot of traffic.
 
I would advise using a database (SQL) instead of a text file. Just slightly modify the code given above. Beware this file can get pretty big if your website gets a lot of traffic.

yeah, db is always better, but the guy probably wants just a text file..
 
Hi guys, I basically want to upload a PHP script to my server that will give me this info for every single person/bot that lands on that page:
user agent, IP address, and referrer.

Any suggestions??

If you don't need to import that information into anything, then your server will probably be logging this information anyway - most hosts offer some kind of webstats program like Awstats or even the option to download raw access log files.
 
add google analytics to your site! :)
This is a horrible suggestion! Sorry, but I'm not looking for newb advice.

Thanks for all the other info on this thread, though, I think that script will work just fine for my needs. Yeah, db is better, but I'm just using this to check the hits I'm getting and then shutting off traffic after maybe 100 visitors. I might log more visitors later, but just wanted something simple like this.
 
Last edited:
This is a horrible suggestion! Sorry, but I'm not looking for newb advice.

Thanks for all the other info on this thread, though, I think that script will work just fine for my needs. Yeah, db is better, but I'm just using this to check the hits I'm getting and then shutting off traffic after maybe 100 visitors. I might log more visitors later, but just wanted something simple like this.

Newb advice?

Google analytics is problaby one of the most detailed and best solutions out there to keep track of your visitors. There is also awstats wich could easily be installed on your server, if you have control over it ofcourse.


why reinvent the weel? Why waste time programming something thats already made and as good as it could get when it comes to stats?

It's totally upto you, but I don't think I'm the "newb" here if you actually have to ask how to make your own statistics script.. I could make that with a blindfold covering my eyes.

Good luck
 
Last edited:
Newb advice?

Google analytics is problaby one of the most detailed and best solutions out there to keep track of your visitors.

It 's also a Google product and you don't want it on your BH sites, do you? ;)
 
Newb advice?

Google analytics is problaby one of the most detailed and best solutions out there to keep track of your visitors. There is also awstats wich could easily be installed on your server, if you have control over it ofcourse.


why reinvent the weel? Why waste time programming something thats already made and as good as it could get when it comes to stats?

It's totally upto you, but I don't think I'm the "newb" here if you actually have to ask how to make your own statistics script.. I could make that with a blindfold covering my eyes.

Good luck
Why am I even wasting my time on this issue? Like I said, you're clearly a newb. First of all, I don't want google knowing shit about my site. Secondly, google analytics doesn't give you numbers on everyone who visits your site. I want to know every bot or spider that crawls my site as well as people who don't have javascript. Many people use javascript blockers so those people won't be reported on your site.

Seriously, I'm trying to help you out here, if you want an alternative to Google Analytics that's self-hosted and free, check out Piwik. Google Analytics is about the worst analytics program you can use on your sites, ESPECIALLY if you're using blackhat methods.
 
Back
Top