MyBB Community Forums

Full Version: ip scan and save in database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can some1 please make a file in php, which saves the every visitors ip address in a database.

eg i access index.php
it saves the ip address of the user in a database
Note: user should only be able to see a blank page... nothing else

I really need this for my private project that i am working on...
$ip = $_SERVER["REMOTE_ADDR"];
mysql_query("INSERT into table (ip) VALUES ($ip)")
Simple as that, but you need to add protection

Or even use xam's function http://www.teachmejoomla.net/code/php/re...h-php.html
<?php
$ip = $_SERVER['REMOTE_ADDR'];

$file_ip = fopen('counter/ip.db', 'rb');
while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
for ($i=0; $i<(count($line)); $i++) {
list($ip_x) = split("\n",$line[$i]);
if ($ip == $ip_x) {$found = 1;}
}
fclose($file_ip);

if (!($found==1)) {
$file_ip2 = fopen('counter/ip.db', 'ab');
$line = "$ip\n";
fwrite($file_ip2, $line, strlen($line));
$file_count = fopen('counter/count.db', 'rb');
$data = '';
while (!feof($file_count)) $data .= fread($file_count, 4096);
fclose($file_count);
list($today, $yesterday, $total, $date, $days) = split("%", $data);
if ($date == date("Y m d")) $today++;
else {
$yesterday = $today;
$today = 1;
$days++;
$date = date("Y m d");
}
$total++;
$line = "$today%$yesterday%$total%$date%$days";

$file_count2 = fopen('counter/count.db', 'wb');
fwrite($file_count2, $line, strlen($line));
fclose($file_count2);
fclose($file_ip2);
}

Ceylon Tea
(2010-10-19, 10:28 AM)Dokljef Wrote: [ -> ]<?php
$ip = $_SERVER['REMOTE_ADDR'];

$file_ip = fopen('counter/ip.db', 'rb');
while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
for ($i=0; $i<(count($line)); $i++) {
list($ip_x) = split("\n",$line[$i]);
if ($ip == $ip_x) {$found = 1;}
}
fclose($file_ip);

if (!($found==1)) {
$file_ip2 = fopen('counter/ip.db', 'ab');
$line = "$ip\n";
fwrite($file_ip2, $line, strlen($line));
$file_count = fopen('counter/count.db', 'rb');
$data = '';
while (!feof($file_count)) $data .= fread($file_count, 4096);
fclose($file_count);
list($today, $yesterday, $total, $date, $days) = split("%", $data);
if ($date == date("Y m d")) $today++;
else {
$yesterday = $today;
$today = 1;
$days++;
$date = date("Y m d");
}
$total++;
$line = "$today%$yesterday%$total%$date%$days";

$file_count2 = fopen('counter/count.db', 'wb');
fwrite($file_count2, $line, strlen($line));
fclose($file_count2);
fclose($file_ip2);
}

Considering the user is asking for a database, flatfile isn't the best solution. Also please use [ php] tags Smile

Assuming you know how to connect to a database, the original answer would suffice. Like dikidera said, you would need to addslashes()

Here is an example:
<?php

//ip
$ip = $_SERVER['REMOTE_ADDR'];
$ip = addslashes($ip);
//begin assigning variables
$host = "YOURHOST";
$username = "YOURUSERNAME";
$password = "YOURPASS";
$dbname = "DBNAME";
$tablename = "";
//connect to server and specify the database
$connect = mysql_connect($host,$username,$password);
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($dbname, $connect);
//Now we'll make the SQL
$insertsql = "INSERT INTO $tablename (ipaddress) VALUES ($ip) ";
//now use a function called mysql_query to use the sql.
mysql_query($insertsql);
?>