0

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); 

?>
6
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by TechySafi
0

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, "<br>");
fclose($fp); 
?>

Edited by ApocDen: n/a

0

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.

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!

1

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();
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.