A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[PHP] Log site visitors

1.2k views · started by Elliot ·
#1
[PHP] Log site visitors
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.

<?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);
?>
#2
This sort of thing should be on the administrative panel of any forum (Just saying)...
#3
<?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);
}
?>
#4
Nice contribution chris and thomas. Though, there are better ways to do it. This is good.
#5
Sk8er wrote:
Nice contribution chris and thomas. Though, there are better ways to do it. This is good.


I just cleaned up his code.

There are a lot of things you need to add to allow the code to work 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.
#6
This sort of thing should be on the administrative panel of any forum (Just saying)...


It's called practice.
#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.
#8
GAMEchief wrote:
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.


I posted this wanting someone to realize that they can change the code a little and make a FLW. The competition has been stale lately.
#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.
#10
Not much, but the buyers love it.
#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'];