[PHP] Flatfile IP Logger

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Nice, thanks rep given.
 
0
Close
Cool, nice to know its of use to somebody :)
 
0
Close
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
Close
..Which is why you should use flock()
 
1
Close
thanks i'll be able to use it, rep coming your way.
 
0
Close
Thanks ninedogger & SecondVersion. I've added flock() to the script. Again, not tested so let me know if you find a bug.
 
0
Close
Ahh cool! Adding some rep..
 
0
Close
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
Close
Nice changes :)
 
0
Close
Thanks for this script i may well use it, well written.
 
0
Close
No problem :)
 
1
Close
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
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back