Posts Tagged ‘ISP’

Hit Tracking with PHP and MySQL

September 3rd, 2008 by smp | Comments | Filed in Technology

Recently there was an outage at a hit-tracking vendor I was using to track the hits on my externally hosted blog, leaving me with a gap in my visitor data several hours long. While this was an inconvenience for me, I realized that this could be mission critical failure to an online business reliant on this data.

To resolve this, I used the PHP HTTP environment variables and the built-in function for converting IP addresses to IP numbers to create my own hit-tracker. It is a rudimentary tracking tool, but it provides me with the basic information I need to track visitors.

To begin, I wrote a simple PHP script to insert tracking data into a MySQL database. How do you do that? You use the gd features in PHP to draw an image, and insert the data into the database.


header ("Content-type: image/png");

include("dbconnect_logger.php");
$logtime = date("YmdHis");
$ipquery = sprintf("%u",ip2long($_SERVER['REMOTE_ADDR']));

        $query2 = "INSERT into logger.blog_log values \
               ($logtime,$ipquery,'$HTTP_USER_AGENT','$HTTP_REFERER')";
        mysql_query($query2) or die("Log Insert Failed");

mysql_close($link);

$im = @ImageCreate (1, 1)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 224, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);

// imageline ($im,$x1,$y1,$x2,$y2,$text_color);
imageline ($im,0,0,1,2,$text_color);
imageline ($im,1,0,0,2,$text_color);

ImagePng ($im);
?>

Next, I created the database table.


DROP TABLE IF EXISTS `blog_log`;
CREATE TABLE `blog_log` (
  `date` timestamp NOT NULL default '0000-00-00 00:00:00',
  `ip_num` double NOT NULL default '0',
  `uagent` varchar(200) default NULL,
  `visited_page` varchar(200) NOT NULL default '',
  UNIQUE KEY `date` (`date`,`ip_num`,`visited_page`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

It’s done. I can now log any request I want using this embedded tracker.

Data should begin flowing to your database immediately. This sample snippet of code will allow you to pull data for a selected day and list each individual hit.


$query1 = "SELECT
                bl.ip_num,
                DATE_FORMAT(bl.date,'%d/%b/%Y %H:%i:%s') AS NEW_DATE,
                bl.uagent,
                bl.visited_page
        FROM blog_log bl
        WHERE
                DATE_FORMAT(bl.date,'%Y%m%d') ='$YMD'
		and uagent not REGEXP '(.*bot.*|.*crawl.*|.*spider.*|^-$|.*slurp.*|.*walker.*|.*lwp.*|.*teoma.*|.*aggregator.*|.*reader.*|.*libwww.*)'
        ORDER BY bl.date ASC";

print "<table border=\"1\">\n";
print "<tr><td>IP</td><td>DATE</td><td>USER-AGENT</td><td>PAGE VIEWED</td></tr>";
while ($row = mysql_fetch_array($result1)) {
        $visitor = long2ip($row[ip_num]);
        print "<tr><td>$visitor</td><td nowrap>$row[NEW_DATE]</td><td nowrap>$row[uagent]</td><td>";

	if ($row[visited_page] == ""){
    	    print " --- </td></tr>\n";
	} else {
    	    print "<a href=\"$row[visited_page]\" target=\_blank\">$row[visited_page]</a></td></tr>\n";
	}

}

mysql_close($link);

And that’s it. A few lines of code and you’re done. With a little tweaking, you can integrate the IP number data with a number of Geographic IP databases available for purchase to track by country and ISP, and using graphics applications for PHP, you can add graphs.

For my own purposes, this is an extension of the Geographic IP database I created a number of years ago. This application extracts IP address information from the five IP registrars, and inserts it into a database. Using the log data collected by the tracking bug above and the lookup capabilities of the Geographic IP database, I can quickly track which countries and ISP drive the most visitors to my site, and use this for general interest purposes, as well as the ability to isolate any malicious visitors to the site.

Tags: , , , , , , , , , , , , , ,

You know you have reached middle age when…

March 9th, 2008 by smp | Comments | Filed in Life, RANTING

You hear a song off Bob Mould’s album Workbook used in a TIAA-CREF commercial.

I think I’ll just nip off and shoot myself.

Tags: , , , , , , ,

Overwaitea: Hey! How about a 20 minute shift?

February 5th, 2008 by smp | Comments | Filed in Life, RANTING, Work

Many, many years ago, I started my depressing voyage through the world of work at an Overwaitea food store in my home town. As a teenager, I expected to work so weird hours, and accept some level of abuse from the “adults” I worked with.

However, it seems that the organization now expects all their employees to accept this crap [here].

A major B.C. grocery chain wants some of its unionized staff to work shifts of just two hours, a move the union representing 8,500 workers called shocking.

The Overwaitea Food Group, which also runs Save-On-Foods and Urban Fare, made the demand for two-hour shifts as it began negotiating a new contract with the United Food and Commercial Workers Union, the union said.

Technorati Tags: ,,

Tags: , , , , , , , , , , , ,

New GrabPERF Measurement Locations

January 11th, 2008 by smp | Comments | Filed in GrabPERF

I know that I have been bad at blogging news about GrabPERF, but today there is some. In the last two weeks, we have added two measurement locations: Washington DC AOL and Argentina LaNacion.

Thanks to Carson Evans of AOL, and Jose Falvo and Leonardo Lancellotta of LaNacion for helping out with the installation process.

Tags: , , , , , , , , , , , , , , , , ,

7 Hours at Sea-Tac on New years Eve

December 29th, 2007 by smp | Comments | Filed in Life

So, on New Years Eve, due to the vagaries of modern air travel, the family will be spending seven hours at Sea-Tac waiting for the second leg of our trip home.

For me, this is usually not an issue, as I can huddle up in a corner with my wireless connection and while away the hours with work and general interest. However, we will be a one laptop family, and my children need to be entertained.

Likely at least an hour of the trip will be handled by immigration as they subject us to the joys of entry with our Advanced Parole documents. It’s now harder for us to get into the US via air as late process Green Card applicants than it is if we were simply visiting the country.

After that, who knows.

Does anyone out there in blog land have any great suggestions for entertaining a family for seven hours at an airport?

Tags: , , , , , , , , , , , , , , , , ,

San Francisco 1, Wasteful Excess 0

December 28th, 2007 by smp | Comments | Filed in Life, RANTING

 

 

Don’t really need to explain this, do we?

Via Core77 and LuxuryLaunches

Technorati Tags: ,,

Tags: , , , , , , , , , ,

There is a 12-step program to help even you…

December 24th, 2007 by smp | Comments | Filed in Life, RANTING

I am often chided for my 5-shot, Venti Latte.

Then I saw this.

Yup, it’s a “a 13 shot Venti soy hazelnut vanilla cinnamon white mocha with extra white mocha and caramel”.

Lord help this person when they wake up in the alley behind Starbucks on Christmas Day.

Original image from Core77.

Technorati Tags: ,,,

Tags: , , , , , , , , , , ,

Dear Sprint (PCS): You suck!

October 16th, 2007 by smp | Comments | Filed in GSM

Samantha’s second Samsung A560 died last week, so I got mad and called Sprint to try and get out of the contract. No way — a kidney and my eldest child is the only way to get out of this deal with the devil.

So, until March 2008, the phone will be powered down and stuck in a drawer. We will grudgingly pay the bill, knowing that we can then tell them to take a long walk off a short pier and not re-subscribe.

On Sunday, we added Samantha to my T-Mobile plan and she now has several fine phones to choose from out of my existing collection. I really don’t see us going back to a CDMA service…ever.

Technorati Tags: , , , , ,

Tags: , , , , , , , , , , , ,

Where there’s smoke, there’s poor fireplace design

September 20th, 2007 by smp | Comments | Filed in Life

The boys wanted a fire last night, so we fired up a pressed log, and all was well.

This morning, Samantha prepared the fireplace for a repeat tonight. We kept smelling the remnants of last nights fire smoldering, so we just lit the thing.

Guess the logs were lined up wrong.

Living Room full of smoke, billowing out the front of the fireplace.

I love home ownership.

Technorati Tags:

Tags: , , , , , , , ,

1 for 1

September 20th, 2007 by smp | Comments | Filed in Canada

This morning, the Canadian Dollar was briefly worth more than the US Dollar on international money markets.

Skating on the River Styx, anyone?

Tags: , , , , , , ,