jump to navigation

Logging Visitor’s IP with PHP January 14, 2012

Posted by Tournas Dimitrios in PHP.

This article presents a simple practical example how to record your visitor’s IP . Optionally a message and  the origin that triggered the record (file-name)  can also be written into the log-file . There are many alternative methods to achieve the same result like : ZF’s Zend_Log , PEAR’s Log package , Klogger , PHP’s native Ligging functions , Apache’s log4php , NmnLogger , Monolog . These are only few of the available solutions that comes right now into my mind . Though ,  I decided to write a class myself , which needs minimal configurations and few lines of code . Even newbies can implement IP-logging into their websites in four simple steps .

Step-1 : Into the root directory of your website create a file  (Class.Logging.log) and paste the following code into it  .

//file Class.Logging.php
Class log {

const USER_ERROR_DIR = "/home/Your_user_account/Logging/Site_User_errors.log";
   Record IP and optionally a message ...
    public function userIP($ip, $msg="No msg", $from="No File-info") {
    $date = date('d.m.Y h:i:s');
    $log = " Date: ".$date." |User's IP : ".$ip." |Message : ".$msg." |File : ".$from."\n";
    error_log($log, 3, self::USER_ERROR_DIR);
   Record only a message ...
	 public function msg($msg) {
    $date = date('d.m.Y h:i:s');
    $log = " Date:  ".$date." |Message : ".$msg."\n";
    error_log($log, 3, self::USER_ERROR_DIR);


Step-2 : Into the root directory of your website create a directory ” Logging” . Here is where our logging text will be recorded ( Site_User_errors.log) . If the file isn’t already created ,our  log-Class will create the file .

Step-3 : Trigger IP-logging . Into any webpage paste the following code n(before the closing HTML “body” tag)  . It will instantiate the Class and trigger the logging functionality .

require_once 'Class.Logging.php';
$msg = "this is another message ";
$from = $_SERVER['SCRIPT_NAME'] ;
$remaddr = $_SERVER['REMOTE_ADDR'];
$log = new log();
$log->userIP($remaddr , $msg , $from);

Step-4 : Only server’s public directory is accesible via a browser ,  root directory can only be accessed via FTP or CPannel .  With a FTP client  or your CPannel’s interface access “Site_User_log” (which is into Logging dir )   and open it with a text editor (Notepad++) . Keep your eyes on the file-size of the logg-file , in regular intervals (once a month ) clear or delete the file . Each simple record takes 400Bytes of your disk-space . Here is a screen-shot of the result .

Both files can have full permissions ( CHMOD 777 ) , these files will not be accessible from the browser as they are located into the “root” directory of your web-page (“/home/Your_user_account/) . Only your public-directory is accessible via a browser (“/home/Your_user_account/public_html “) .


1. Build a Visitor Tracking System for your Website with PHP « Tournas Dimitrios - January 15, 2012

[…] previous article “Logging Visitor’s IP with PHP  ” was a simple example of logging visitor’s location into a “txt” file . […]

2. Nick Rowe - May 17, 2012

Hey I’ve tried implementing this test but am so far unable to get this working successfully. All files have been created and php is useable on the server as we have forms utilizing it elsewhere on the main site.

I’ve followed the above instructions as best i can but am not a bit lost , please could give me some guidance.

Regards Nick

tournasdimitrios1 - May 17, 2012

I need more details to understand your subject .
1)Is the “Class.Logging.php” file uploaded correctly into PHP’s include path ?
Create a file with the following code :
// Works as of PHP 4.3.0
echo get_include_path();

// Works in all PHP versions
echo ini_get(‘include_path’);
PHP’s get_include_path will point you the direction .
2)Into a private (non-publicly accessible)location of your PHP’s-include path , create a directory (folder “LOgging”) with full permissions (CHMOD 777 ) . You have not to worry about security issues as this directory isn’t publicly accessible .
3)Don’t forget to edit the path of your “Logging” folder in the Class.Logging.php . For example on my server the configuration is as follows :
const USER_ERROR_DIR = “/home/a7390405/Logging/Site_User_errors.log”;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s