954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Ip logger

0
By Denis Sellu on Aug 2nd, 2010 6:50 pm

the code is very simple and all it does is get the ip of a member or guest and logs it in a text file. if the log.html doesn't exist it will auto create one for you, as long as the path is correct. Also make sure you set proper CHMOD of 0755

<?php
// Logs the ip address of guests/members of your site.

// add the full path to the log file
$logfile= '/path/log.html';

// gets the ip
$IP = $_SERVER['REMOTE_ADDR'];

// logs the date 
$logdetails=  date("F j, Y, g:i a");


// open the log.html and adds the entry
$fp = fopen($logfile, "a"); 
fwrite($fp, $logdetails);
fwrite($fp, "<br>");
fclose($fp); 

?>

You forgot to write the $IP to file ...

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

yes thank you i will fix that now

ApocDen
Junior Poster
126 posts since Jul 2010
Reputation Points: 11
Solved Threads: 13
 

I have made a few changes to the file.

change log.
you done need to write in the full file path.
the $directory_self searches where the file was uploaded and create / save the log.html in the same directory.

make sure the file that contain the code in CHMOD 755 , otherwise the file wont be created or it wont be able to open so the ips can be saved to it.

<?php
// Logs the ip address of guests/members of your site.

// add the full path to the log file

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

$logfile= $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'log.html';

// gets the ip
$IP = $_SERVER['REMOTE_ADDR'];

// logs the date 
$logdetails=  date("F j, Y, g:i a") . ' : ' . $IP ;


// open the log.html and adds the entry
$fp = fopen($logfile, "a"); 
fwrite($fp, $logdetails);
fwrite($fp, "");
fclose($fp); 
?>
ApocDen
Junior Poster
126 posts since Jul 2010
Reputation Points: 11
Solved Threads: 13
 

Lets imagine that i had a site with text input and "ok" button next to it. How could I log the thing that my visitor writes in text input + his ip. I wouldn't like to log just visitor ips i would like to log ones who write something. I am sorry if my post was obscure.

errrrrrrrno
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I'm working on a security system on my website. Is there a way to use this script with like log.php and when they go into an office have it show that they logged into the office, viewed user information, etc?

Thank you!

mfoland
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

I have one for insertion into MySQL when someone register, just PM me if you need.

Sorcher
Junior Poster
191 posts since Oct 2010
Reputation Points: 16
Solved Threads: 9
 

Instead of just this

$IP = $_SERVER['REMOTE_ADDR'];

Id go further with this

function getRealIpAddr() {
  if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip=$_SERVER['HTTP_CLIENT_IP']; // share internet
  } elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; // pass from proxy
  } else {
    $ip=$_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}
$ip = getRealIpAddr();
TechySafi
Junior Poster in Training
99 posts since Oct 2010
Reputation Points: 10
Solved Threads: 14
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You