Unstoppable Domains

[PHP] Flatfile IP Logger

Spaceship Spaceship
Watch

tm

Established Member
Impact
25
I just wrote a flatfile IP logger and would like to share it with you guys in case you find it useful :)

Set $variable to whatever you want to log.

PHP:
$logfile = "LOG.txt"; //change this line if you want to change the file it writes to (must exist and CHMOD 0777)
$IP = $_SERVER['REMOTE_ADDR']; //sets the variable for $IP (gets the ip address)
$data = "$variable => $IP  \n"; // sets the text to write to $logfile & adds a new line for the next one (nice and clean)
$fp = fopen("$logfile", "a"); //opens the log file
flock($logfile, LOCK_EX); // start an exclusive lock
fwrite($fp, $data); //writes to the log file
flock($fp, LOCK_UN); // release lock
fclose($fp); //closes the logfile (important!)
	echo "<br /><br />Your IP ($IP) has been Logged with accordance of our T.O.S."; //tells people the've been warned!

I might come back with a mySQL one later.
 
Last edited:
3
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Nice, thanks rep given.
 
0
•••
Cool, nice to know its of use to somebody :)
 
0
•••
just always wanted to know this, but

what happens if 2 people go onto a site with this at the exact same time?
will they both get logged or just one

i know like mysql should get both, cuz they process in sequential order or w/e blah

same with FF?
 
0
•••
..Which is why you should use flock()
 
1
•••
thanks i'll be able to use it, rep coming your way.
 
0
•••
Thanks ninedogger & SecondVersion. I've added flock() to the script. Again, not tested so let me know if you find a bug.
 
0
•••
Ahh cool! Adding some rep..
 
0
•••
Nice script tm!

I modified it a bit for my personal use ;)
- It now adds the date to the log file - "May 4, 2006, 6:36 pm - 12.123.12.123"
- I can enter using http://www.SITENAME.com/index.php?user=me and it doesn't log my IP.
- Also by using that address it welcomes me back. lol :)

PHP:
$logfile = "log.txt"; //change this line if you want to change the file it writes to (must exist and CHMOD 0777)
$IP = $_SERVER['REMOTE_ADDR']; //sets the variable for $IP (gets the ip address)
$today = date("F j, Y, g:i a");
$data = $today." - ".$IP."\n"; // sets the text to write to $logfile & adds a new line for the next one (nice and clean)
$user = $HTTP_GET_VARS['user'];

if ($user == "me"){
	echo "welcome back!<br />";
} else {
	$fp = fopen("$logfile", "a"); //opens the log file
	flock($fp, LOCK_EX); // start an exclusive lock
	fwrite($fp, $data); //writes to the log file
	flock($fp, LOCK_UN); // release lock
	fclose($fp); //closes the logfile (important!)
}
 
Last edited:
0
•••
Nice changes :)
 
0
•••
Thanks for this script i may well use it, well written.
 
0
•••
No problem :)
 
1
•••
I've been looking for decent IP logger for a while, I'm also terrible at using file handlers in PHP.. almost all my scripts use a database; thanks :). Rep +.
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back