$ip='59.93.102.188';
echo ip2long($ip);
// output of this will be 995976892
We can restore the original octal values by using long2ip function. Here is an example.
$var=995976892;
echo long2ip($var);
// Output of this will be 59.93.102.188
select inet_aton('59.93.102.188')
The output will be 995976892select inet_ntoa('995976892')
The output will be 59.93.102.188
Field Size: To store IPV4 address we can keep the field length as 15 ( 4x3 plus 3 separators ). To store IPV6 address we have to keep 39 as field length ( 4x8 plus 7 separators ). To map IPV4 to IPV6 address you have to keep 45 as field length. At present we can go with IPV4 address length
Storing visitor details in MySQL table We will store details in a database table after collecting the variable. The table structure you can download from here. Here are the details.$ref=@$_SERVER[HTTP_REFERER];
$agent=@$_SERVER[HTTP_USER_AGENT];
$ip=@$_SERVER['REMOTE_ADDR'];
$tracking_page_name2 = $_SERVER["SCRIPT_NAME"];
$tracking_page_name = 'P_Page_name';
$strSQL = "INSERT INTO track( ref, agent, ip,tracking_page_code, tracking_page_name) VALUES (:ref,:agent,:ip,:tracking_page_code,:tracking_page_name)";
You can read how to insert data to mysql table here.
This code is kept inside a footer file or a common file which is used in all pages. So if you are not connected already to database then you can use database connection string to establish connection.
require "config.php";
We have used one if condition checking to insert record if referrer of the visitor is present. We will also check that referrer is not internal pages of the site.
if(strlen($ref) > 2 and !(stristr($ref,"plus2net.com"))){ // exclude referrer from your own site.
Here is the complete code
$ref=@$_SERVER[HTTP_REFERER];
$agent=@$_SERVER[HTTP_USER_AGENT];
$ip=@$_SERVER['REMOTE_ADDR'];
$tracking_page_name2 = $_SERVER["SCRIPT_NAME"];
if(strlen($ref) > 2 and !(stristr($ref,"plus2net.com"))){ // exclude referrer from your own site.
$strSQL = "INSERT INTO track( ref, agent, ip,tracking_page_code, tracking_page_name) VALUES (:ref,:agent,:ip,:tracking_page_code,:tracking_page_name)";
$sql=$dbo->prepare($strSQL);
$sql->bindParam(':ref',$ref,PDO::PARAM_STR, 250);
$sql->bindParam(':agent',$agent,PDO::PARAM_STR, 250);
$sql->bindParam(':ip',$ip,PDO::PARAM_STR, 20);
$sql->bindParam(':tracking_page_name',$tracking_page_name2,PDO::PARAM_STR, 100);
$sql->bindParam(':tracking_page_code',$tracking_page_name,PDO::PARAM_STR, 100);
if($sql->execute()){
// Part of the code to execute after successful execution of query
//echo " Success ";
}
else{
// Part of the code to execute if query fails ///
//print_r($sql->errorInfo());
}
///////////// inserted details
}
The above code will insert the visitor details to the table name track in mysql database.
<?Php
$tm=time();
$ref=@$_SERVER[HTTP_REFERER];
$agent=@$_SERVER[HTTP_USER_AGENT];
$ip=@$_SERVER['REMOTE_ADDR'];
// opening the file with file pointer at end of the file
$my_file = fopen("visitors.csv","a");
fputcsv($my_file,array($tm,$ref,$agent,$ip));
fclose($my_file);
?>
By using fopen function we are opening a file, if the file is not already created then it will create the file. The mode of opening the file is a, so the pointer will remain at the end of the file. So each time a new visitor comes to the page , a new line will be added at the end.
$file_name="visitors_".date("m-d-y").".csv";
// opening the file with file pointer at end of the file
$my_file = fopen($file_name,"a");
Now you will get one file for each day storing your visitors details.
CREATE TABLE IF NOT EXISTS `track` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`ref` varchar(250) NOT NULL DEFAULT '',
`agent` varchar(250) DEFAULT '',
`ip` varchar(20) NOT NULL DEFAULT '',
`tracking_page_code` varchar(100) DEFAULT '',
`tracking_page_name` varchar(100) DEFAULT '',
`dt` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
IP address and Geo Location of visitorCollecting page referrer
steve | 30-11-2009 |
I would like to log my visitors ip address into a flat file. |
laurence | 18-01-2010 |
what is the point of Storing as decimal number? what do you gain from doing that as opposed to storing ip in the normal format? |
sayar ahmad kuchy | 27-01-2010 |
i like this its very informative article.... |
Sam | 21-10-2011 |
can you save as a csv? |
Smo | 04-01-2015 |
can be stored in csv files, details on how to store is added to the article now. |
Jitender | 08-01-2015 |
I want to save visitors information in mysql table not in csv file. And i also want to give permission to access files to some IP addresses only. Is that its possible.. |
smo | 09-01-2015 |
Details on storing visitor details in MySQL table is added. Permission part of the file we will discuss separately. |