Tag Archives: development

Capture Visitors Location w/ PHP

Capturing your visitors location is great for many reasons while building statistical software, its also good just to know where people are that are visiting your website. Heres a quick and dirty way for capturing location data.

Download theĀ IP2Country CSV File unzip it and fire it into your FTP dir, we need to gather all the data out of it and stick it into a MySQL database.

SQL Table
[php]
CREATE TABLE `country_ip` (
`ip_from` DOUBLE NOT NULL ,
`ip_to` DOUBLE NOT NULL ,
`country` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM
[/php]

Then we create the file that will import all the information.

[php]
$file_arr = file(‘ip-to-country.csv’);
foreach($file_arr as $line){
$clean_line = trim(str_replace(‘”‘, “‘”, str_replace(“‘”, “\'”, $line)));
$sql = mysql_query(“INSERT INTO `country_ip` (`ip_from`,`ip_to`,`country`) VALUES($clean_line)”);
}
echo ‘Finished :]’;
[/php]

Once you have ran the above code, it should insert all the data into your mysql database, you can go ahead and delete the ip-to-country.csv file now.

Next all we are doing is querying the database to find out the current visitors location, you can then do whatever you like with the data.

[php]
$ip = ip2long($_SERVER[‘REMOTE_ADDR’]);
$ipc_q = mysql_query(“SELECT * FROM `country_ip` WHERE `ip_from` <= '".$ip."' AND `ip_to` >= ‘”.$ip.”‘”);
$ipc = mysql_fetch_array($ipc_q);
[/php]

When someone visits paulOr.net quite a lot of details are being put into the database regarding each visitor, for example, I work out if the user has been on in the past 24 hours (unique), capture the users IP address, each page visited, visitors country and time/date of visit. From this information, I have created some nice analytical apps in the backend of paulOr.net to feast my eyes upon.

Showing Server Load As A Graph With PHP

Along side yesterdays blog post regarding Tweeting out your server status, there are more proactive ways of keeping an eye on whats happening. One of my favorites is showing the server load as a graph/progress bar thing.

Showing the server load alone is pretty nice, however with a little help from jQuery, we can make it bloody epic! – Showing it in real time.

index.php
[php]


[/php]

load.php
[php]
$int)
$status_i[$k]=(($int<0.25)?'green':(($int<0.75)?'orange':'red')); ?>

Current Load: %