Post: [TuT] IP Logger to Database or External File [TuT]
05-10-2011, 12:56 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

This will be a IP logger which grabs the IP, Browser, Date, Host/ISP, and URL of where it collected this data in 2 ways:
-Via MySQL
-Via External file

Difficulty: Very Easy (1/10)
Reason: To create a website which grabs information from the visitors
Past knowledge: Very basic PHP & Copy and Paste.
Materials: A mysql database (phpMyAdmin), and a website.
Possible uses: Truthfully, I have no real use for it. Unless I want to get someone's dox, I can't find a reason. If you have a use, go right ahead and use it.

[size=x-large][b][align=center][color=#FFD700] MySQL Method[/color][/align][/b] [/size]


First off, we are going to start by defining the database connection variables (Like always). Replace the value inside the quotes with the actual value:
    
<?php
//Database Variables
$host = "db_host"; //The host where the database is held/hosted
$username = 'db_user'; //The username used to login to the database
$password = 'db_pass'; //The password to your database.
$database = "db_name"; //Name of the database you are accessing


Now we will now define the variables of the information that we are getting (IP, Browser, Date, Host/ISP and URL):
    
//To-Steal Variables
$ip = $_SERVER['REMOTE_ADDR']; //Gets the IP adress of the visitor
$browser = $_SERVER['HTTP_USER_AGENT']; //Gets the Broswer of the visitor
$date = date("F j, Y, g:i a"); //The date for when this was recorded
$fhost = gethostbyaddr($ip); //Finds the full host of the IP adress
$host = preg_replace("/^[^.]+./", "", $fullhost); //Gets only the host from the full host
$url = “http” . ((!empty($_SERVER['HTTPS'])) ? “s” : “”) . “://”.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; //The URL in which the information was grabbed from


Obviously, now we should connect to the database:
    
$connect = mysql_connect($host,$username,$pwd) or die("Connection to database failed.");
mysql_select_db($database, $connect);


Write the variables to the table so it will be stored. (Column1-5 are to be replaced by the name of the columns... In the order of IP, Browser, Date, Host, then URL.)
    
mysql_query("INSERT INTO table_name (column1, column2, column3, column4, column5) VALUES ('$ip', '$browser', '$date', '$host', '$url'Winky Winky");


Now we have to end the program.
    
mysql_close();
?>


Finally, post this on your website & see the information pour in!

[size=x-large][b][align=center][color=#FFD700] External File Method[/color][/align][/b] [/size]

We will start off by
    
<?
// To-Get Variables
$ip = getenv('REMOTE_ADDR'Winky Winky; //Gets the IP adress of the visitor
$date = date("D M j G:i:s T Y");
$browser = $_SERVER['HTTP_USER_AGENT']; //Gets the Broswer of the visitor
$site = “http” . ((!empty($_SERVER['HTTPS'])) ? “s” : “”) . “://”.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; //The URL in which the information was grabbed from
$fhost = gethostbyaddr($ip); //Finds the full host of the IP adress
$host = preg_replace("/^[^.]+./", "", $fullhost); //Gets only the host from the full host


Organization of the new file being written should be written below the above variables (this will be written in the new file):
    
$head = "|| ----- IP Log ----- || \n\n";
$logt = "Site: $site \n";
$logl2 = "IP: $ip \n";
$logl3 = "Date: $date \n";
$logl4 = "Browser: $browser \n";
$logl5 = "Host: $host \n";
$loge = "\n";
$log = "$logt $logl2 $logl3 $logl4 $logl5 $loge";


This step writes the file & Checks to see if it exists. You should just post this after the variables above:
    
$myFile = "log.txt";
if (filesize($myFile) <= 1){
$fh = fopen($myFile, 'w'Winky Winky;
fwrite($fh, $head);
} else {
$fh = fopen($myFile, 'a'Winky Winky;
}
fwrite($fh, $log);


Not to close the file and finish it up.
    
fclose($fh);
?>


Finally, upload it to a page on your website and every visit will be logged!

[align=center]:glare: This being my first tutorial, it is a very simple one. Please don't be too harsh. Sorry if this tutorial is useless :glare:[/align]


source You must login or register to view this content.
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo