Ip logger

ApocDen 0 Tallied Votes 1K Views Share

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

?>
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

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

ApocDen 0 Junior Poster

yes thank you i will fix that now

ApocDen 0 Junior Poster

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); 
?>
errrrrrrrno 0 Newbie Poster

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.

mfoland 0 Newbie Poster

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!

Sorcher 6 Junior Poster

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

Member Avatar for TechySafi
TechySafi

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.