Log user ip address, date and time
Asked Answered
D

5

13

Is there a simple script or piece of code I can add to my page to keep a log of every visitor, the date and time they hit the page and IP address? And what would be the best way to do this... javascript, php, something else?

EDIT:

Ouch...

Here is what happened... When I went to my server with FileZilla there were all the domain names (about 20) I have being logged like my domain.com so I found the one I needed and checked the logs but it was mainly search engines.

But I just went back and happened to scroll down to stuff that was out of view and there were all the domain names again with www in front like www.mydomain.com and of course the logs in there are huge and have every single bit of info I need.

This happened because I found what I was looking for mydomain.com and of course I stopped looking. I didn't know or see there was a whole other set out of view... honest mistake.

I am still using that code because it is nice and small, the logs are freakin' huge and take hours to download and look at.

Dele answered 26/7, 2011 at 23:3 Comment(5)
Can't you just look at your web server's access logs?Stylo
To answer your questions: (1) yes (2) server-sided, so php or any other server-sided language, and finally lots of choiceAromaticity
Although D.N.'s option is indeed superior. Lots of packages for to automatically parse access logs around also.Aromaticity
I did that and it wasn't showing me all. it was just search enginesDele
If you're running a default apache installation on debian or ubuntu, try checking /var/log/apache2/access.log to see if this file already existsSapphire
R
43
$line = date('Y-m-d H:i:s') . " - $_SERVER[REMOTE_ADDR]";
file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND);

Consider also logging $_SERVER['REQUEST_URI'] or other interesting information, possibly in a more standard format as outlined by @Day.

Ress answered 26/7, 2011 at 23:8 Comment(8)
@gravityboy: It works but as several people have suggested in comments, these 2 lines of code are 2 more lines of code than you really need ;) Try using your webserver's built-in logging instead.Lidia
@Lidia Totally agreed. Depending on your host you may not have access to those logs though, so this is the most straight-forward way of rolling your own.Ress
@Ress Gosh of course. Shared hosting. It's been so long I'd actually forgotten about it. Good point thanks.Lidia
@Ress How do I add the $_SERVER['REQUEST_URI'] into your code?Dele
@gravity Put it into the string after the REMOTE_ADDR. … $_SERVER[REMOTE_ADDR] - $_SERVER[REQUEST_URI]".Ress
Can we get the page they arrived from? That's the referrer? What else is do-able?Dele
@gravity Yes, that's the referer [sic]. Log whatever you want from the $_SERVER array or any other kind of data PHP has access to.Ress
This may be a dumb question but where is visitors.log being written to? I've checked everywhere... in var/www/ in var/log/.... not in root either...Retrospect
A
4
<?php
    // include this piece of code in every page call

    // write in database row
    $log = array('time' => time(), 'ip' => $_SERVER['REMOTE_ADDR'], 'url' => $_SERVER['REQUEST_URI']);
?>
Adigun answered 26/7, 2011 at 23:10 Comment(3)
Does this automatically create the log file?Dele
No. I was assuming you want to write log entries into a database – for logging in files, check @Deceze's answer above.Adigun
long time ago, but which database? you haven't specified a any?Intitule
L
4

The simplest piece of code to add to your page is no code at all. So might I suggest "something else"? Try using your webserver's built-in request logging facility instead of writing some custom PHP code.

Apache and many other webservers can produce logs in the Common Log Format (CLF) and there are many tools available to analyse such logs and draw pretty graphs for you (Webalizer, Awstats etc). A CLF log line looks like this which gives you all the information you asked for and more:

127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 

See the appropriate bit of documentation for your webserver for how to configure logging and give it a whirl:

Lidia answered 27/7, 2011 at 0:0 Comment(3)
My webserver is creating logs but it was not all of the users... it was mainly just search engines. I'll find out why and report back. thanksDele
@Dele That sounds very odd. Which webserver are you using? Apache?Lidia
@Dele Fair enough no problem. Now you'll remember and next time know what to do :)Lidia
C
2

here is my little script to log ip addresses dont forget to add the below after the /HEAD tag also note to make this work it must be a PHP not HTML

<?php include ('log-ip.php') ?>

where ever you want it called from

"log-ip.php"

<?php
$iplogfile = 'logs/ip-address-mainsite.html';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\n<br><br>");
fclose($fp);
?>

and the resault is a nice web HTML log file logs/ip-address-mainsite.html

<!DOCTYPE html><!-- HTML5 -->

<head>
<body bgcolor="#000000">
<title>NZ Quakes - Main Web Site Log</title>

</head>

<body>
<font color="#7FFF00">
<center>NZ Quakes - Main Web Site Log</center>
<font color="gold">
<br><center>
[01/04/2017 08:25:21]: 124.197.9.181 /index.php Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
<br><br>

below is a picture of what it looks like.

enter image description here

what do you think about this i think its clean and simple sort of.

Cephalonia answered 2/4, 2017 at 0:40 Comment(0)
C
1

Most comprehensive - Apache's access log: Log Files -> Access Log @ httpd.apache.org

Circumfluous answered 26/7, 2011 at 23:6 Comment(3)
Implying that the post author uses Apache.Adigun
@Daniel: Well, he was asking for "something else".Circumfluous
@Daniel, well he might not be using Apache, but he's presumably using a webserver, and if his chosen webserver doesn't do the minimal request logging that he's looking for, then it's not a webserver that I've ever heard of. Have expanded on this in my answer.Lidia

© 2022 - 2024 — McMap. All rights reserved.