Any good webmaster knows that it is important to keep logs of everything that happens on his site. So here is come quick PHP code I wrote to help you log the ip, time, and date of each site view.
Code:<?php $ip = $_SERVER['REMOTE_ADDR']; $time = date("g:ia"); $date = date('m:d:y'); $file = 'log.txt'; $handle = fopen("$file", "a"); fwrite($handle, 'Ip = '); fwrite($handle, "$ip \n"); fwrite($handle, 'Time = '); fwrite($handle, "$time \n"); fwrite($handle, 'Date = '); fwrite($handle, "$date \n"); fwrite($handle, '');; fclose($handle); ?>
Results 1 to 11 of 11
Thread: [PHP] Log site visitors
- 27 Dec. 2009 02:08am #1
[PHP] Log site visitors
Last edited by Elliot; 27 Dec. 2009 at 02:10am. Reason: typo
// Signature
- 27 Dec. 2009 02:32am #2
This sort of thing should be on the administrative panel of any forum (Just saying)...
- 27 Dec. 2009 09:13am #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 100.00
PHP Code:<?php
$File = "Log.txt";
$IP = @$_SERVER['REMOTE_ADDR'];
$Time = date("m-d-y");
$Date = date("g:ia");
if($Handle = @fopen($File, "a")){
@fwrite($Handle, "IP = {$IP}\r\nTime = {$Time}\r\nDate = {$Date}\r\n");
@fclose($Handle);
}
?>
- 27 Dec. 2009 06:44pm #4
- 29 Dec. 2009 12:22am #5
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 100.00
I just cleaned up his code.
There are a lot of things you need to add to allow the code trk effeciently.
Mainly look towards checking for file locking issues (Could lead to multiple problems,) and stuff along that lines.
Remember if you have a decently active site you will be getting hundreds of hits a minute, imagine that all in logs. There goes your HD space.
- 30 Dec. 2009 09:31am #6
- 30 Dec. 2009 08:26pm #7
Most sites (and all forums) already have this built in. It's pretty useless if you're not telling where they are or what they were doing. You just know they visited - some anonymous user did something, anything. It's useless for large sites, not to mention the huge toll this would take after a million visitors. Your log file would enter the gigabytes - as if you'd read it all to figure out who did what.
It's a good start to introduce webmasters to REMOTE_ADDR (don't forget X_FORWARDED_FOR), but this specific code shouldn't be used. If it's the only PHP on your site, you don't have a reason to log visitors - no one is going to exploit your HTML. And if you are using something exploitable, you're better off making such logs in user accounts or other databases/tables.
- 30 Dec. 2009 09:37pm #8
- 30 Dec. 2009 09:51pm #9
I'm not sure how IP logs have anything to do with FLWs. Sure an FLW can log IPs, but I don't see the point of doing so.
- 30 Dec. 2009 11:01pm #10
Not much, but the buyers love it.
Last edited by Elliot; 30 Dec. 2009 at 11:06pm.
// Signature
- 30 Dec. 2009 11:37pm #11
Which only goes to show you their ignorance of the matter. >_>
Oh well.
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = 'IP Address: ' . $_SERVER['HTTP_X_FORWARDED_FOR'] . '; Proxy IP Address: ' . $_SERVER['REMOTE_ADDR'];
else $ip = 'IP Address: ' . $_SERVER['REMOTE_ADDR'];