...

View Full Version : IP Logger



m7d7g7
02-12-2003, 05:26 PM
i'm looking for a simple PHP script that when someone goes to it, it will log the users IP and Time in a log.txt file. i looked around this board and other PHP scripting sites and havent found anything.

thanks,
mike :thumbsup:

Spookster
02-12-2003, 05:40 PM
<?php
$file_name = "accesslog.txt";
$data = $REMOTE_ADDR.",".date("l, d-M-Y H:i:s T")."\n";

$file_pointer = fopen($file_name, "a");
$lock = flock($file_pointer, LOCK_EX);

if ($lock){
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
?>


enjoy

m7d7g7
02-12-2003, 05:57 PM
your are awesome!
thanks for your help :)

mike

Feyd
02-12-2003, 07:02 PM
Spookster, oh you wonderful and almighty and all-powerful PHP god! Could you please be so kind if it is not too much trouble your royalness if you could not use double quotes when you don't need them, you're wasting your valuable time...if the string/variable does not need to be parsed for PHP rendered items, then you should enclose it in single quotes since PHP knows that single quotes means "nothing to modify in this string, move along" where double quotes would tell PHP to look for anything in this string which could be modified and then modify it...thus, if there is nothing to modify in that string, and you are still looking for it, then you are wasting your precious time when you could be doing many other wonderful things like giving me lessons on how to program in PHP.

So slightly updated code would look like:


<?php
$file_name = 'accesslog.txt';
$data = $_SERVER['REMOTE_ADDR'] . ',' . date('l, d-M-Y H:i:s T') . "\n";

$file_pointer = fopen($file_name, 'a');
$lock = flock($file_pointer, LOCK_EX);

if ($lock) {
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
?>


Notice only the newline command has double quotes, and that is so that it will be parsed as a newline command, rather than just the text output.

mordred
02-12-2003, 07:26 PM
"Premature optimization is the root of all evil." :D

Have you any benchmarks concerning the use of double quoted strings opposed to single quoted ones? And, how often are they really the bottleneck that eats away system resources? In spooksters code I'd rather say the time-consuming operation will be the writing to the file, and I highly doubt that the time gain of your optimization will be more than some microseconds...

*ducks for cover*

Feyd
02-12-2003, 07:32 PM
True, it isn't that noticeable in a human measure of time, but it can be ages in machine time. Besides, I typically deal in huge volumes of code, thousands of lines...small little lags add up, greatly.

Regardless of that, it is best practice to code efficiently...no matter what it is you are coding. I'm not saying go back and re-write all your scripts...more along the lines of, in the future, make it easier on yourself by a few slight alterations.

mordred
02-12-2003, 08:16 PM
I actually do use single quotes in my php scripts. And yes, the scripts I deal with can get quite large, comparable to yours. Though then it's more likely to be a collection of objects and the real problems with execution time come not from double quoted strings, but that's not my point.

For me, this single quote issue is one of those little tricks that are close to "premature optimization". Do you perform mathematical calculations solely with bitshifting? Do you store preferences in bitfields or do you consider writing a container class for it? Both approaches may have their justifications, and neither is the perfect one. It depends on what you want to achieve - if your application is in dire need of a lightning-fast execution time, then optimize where you can, but the goal might be differently set. Like developing an easy to use application framework.

Last but not least, a great advantage of using single quoted strings is that within those string HTML attributes can be surrounded by double quotes and left so in your code untouched. Makes it easier for me to incorporate quickly some changes in the markup I get from the designer.

Spookster
02-12-2003, 11:59 PM
Originally posted by Feyd
Spookster, oh you wonderful and almighty and all-powerful PHP god! Could you please be so kind if it is not too much trouble your royalness if you could not use double quotes when you don't need them, you're wasting your valuable time...if the string/variable does not need to be parsed for PHP rendered items, then you should enclose it in single quotes since PHP knows that single quotes means "nothing to modify in this string, move along" where double quotes would tell PHP to look for anything in this string which could be modified and then modify it...thus, if there is nothing to modify in that string, and you are still looking for it, then you are wasting your precious time when you could be doing many other wonderful things like giving me lessons on how to program in PHP.

So slightly updated code would look like:


<?php
$file_name = 'accesslog.txt';
$data = $_SERVER['REMOTE_ADDR'] . ',' . date('l, d-M-Y H:i:s T') . "\n";

$file_pointer = fopen($file_name, 'a');
$lock = flock($file_pointer, LOCK_EX);

if ($lock) {
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
?>


Notice only the newline command has double quotes, and that is so that it will be parsed as a newline command, rather than just the text output.

lol Yeah, come in my forum and talk smack and see what happens. Bow down before me swamp slime. :D

Feyd
02-13-2003, 01:02 AM
Hmmm...no?

I absolutely love your edits, by the way....you RAMF :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum