Simple IP logging script

I would like to make a simple IP logging script.

I would like to store the IPs within a mysql database.

I would then like to be able to check to see if an IP is within the database via an if statement.

if ip is in db {
echo “its there”;
} else {
echo “it’s not”;
}

I would like to do this:


$logfile= 'log.txt';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails= date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].' target=_blank>'.$_SERVER['REMOTE_ADDR'].'</a>';

// open the file for reading and writing
$fp = fopen($logfile, "r+");
// write out new log entry to the beginning of the file
fwrite($fp, $logdetails, strlen($logdetails));
fclose($fp);

but with a database - as ill be logging quite a few… Thanks! :smiley:

Here is another example but it doesn’t use a database:


<?php

function logIP()
{
     $ipLog="logfile.htm"; // Your logfiles name here (.txt or .html extensions ok)

     // IP logging function by Dave Lauderdale
     // Originally published at: www.digi-dl.com

     $register_globals = (bool) ini_get('register_gobals');
     if ($register_globals) $ip = getenv(REMOTE_ADDR);
     else $ip = $_SERVER['REMOTE_ADDR'];

     $date=date ("l dS of F Y h:i:s A");
     $log=fopen("$ipLog", "a+");

     if (preg_match("/\\bhtm\\b/i", $ipLog) || preg_match("/\\bhtml\\b/i", $ipLog))
     {
          fputs($log, "Logged IP address: $ip - Date logged: $date<br>");
     }
     else fputs($log, "Logged IP address: $ip - Date logged: $date\
");

     fclose($log);
}
// Place the below function call wherever you want the script to fire.
logIp();


?>

I just found this:

<?
mysql_connect("localhost","username""pass");
mysql_select_db("dbname");

$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO IpLogs (Ip) VALUES ('$ip')") or die(mysql_error());
echo '<br>IP logged.';
?>

But now I need to know how to do that if statement to see if the IP is already in the DB:

if ip is in db {
echo “its there”;
} else {
echo “it’s not”;
}

something like this?


<?
mysql_connect("localhost","username""pass");
mysql_select_db("ips");

$ip = $_SERVER['REMOTE_ADDR'];

$resultx = mysql_query("SELECT * FROM tablename WHERE ip = $ip LIMIT 1");
$nrowsx  = mysql_num_rows($resultx);

if (nrowsx == 0) {
mysql_query("INSERT INTO IpLogs (Ip) VALUES ('$ip')") or die(mysql_error());
echo 'not in db, logging ip.';
} else {
echo 'in db already';
}

?>

The last one is good if you are using Database table to store the IP Addresses. But I am not sure what type of ur ip field is, if it is varchar then try to put the value inside the single quote.


......
$resultx = mysql_query("SELECT * FROM tablename WHERE ip = '$ip'");
......

rest of the code should work for you. And why you are using LIMIT in the select query? Is it really necessary?

Would using limit here speed up your query?
My thinking behind it is, once the record is found, it would stop looking, as its reached its limit. Am I correct in assuming this?

You should be using PDO for database connections.

You should be using PDO for database connections.

Why? Could you please elaborate?

Ps magnam you missing a ‘,’ in your mysql_connect


// this line
mysql_connect("localhost","username""pass");

// should be
mysql_connect("localhost","username","pass");

A silly thing to point out, but maybe it helps you when you cant figure out why your code isn’t working :wink:

It offers more functionality, such as methods to prepare queries and better error handling. If for some reason you decide to use a different database you also don’t have to worry about using third party functions to handle your connection. Also because its a class you can, if needed, extend it, exend the PDOException for custom error handling or use it with the Relection API.

PDO supports a number of databases.

YOu could easily say “but its a simple script, why bother?”. Well my answer is its good practice.

If PDO is available … use it :slight_smile:

Cool nice answer :wink: