CodeIgniter Log Info Library
Actually, Loginfo isn't specific to CodeIgniter, but I wrote the Loginfo class to gather log information for this site. Like most of my PHP stuff, Loginfo is an aggregation of stuff found here and there, so isn't terribly original.
What it does:
- attempts to get the 'real' IP of the visitor, in case they are behind a proxy
- does a very basic browser detection
- records the refering page
- does an IP location lookup on first visit, setting a location cookie to prevent un-needed additional lookups if the IP hasn't changed
CodeIgniter Usage:
Put the Loginfo.php file in the application/Library directory
You probably would autoload a class like this in config/autoload.php -
$autoload['libraries'] = array('loginfo','otherlibraries');
In a controller, in a function that sends the visitor to a view ,for example 'index', instantiate a loginfo object, optionally passing it a page reference that can be recorded in your log, eg:
function index()
{
$info_to_log = new Loginfo('homepage');
$this->log_model->log_hit($$info_to_log);
}
And you'll need a model that saves the loginfo to your database, lets call it log_model.php and create this function:
function log_hit($info_to_log)
{
if(!$this->has_hit($info_to_log))
{
foreach($info_to_log as $k => $v){
$this->db->set("$k", "$v");
}
$this->db->insert('your_log_table_name');
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
}
A private function named 'has_hits' prevents duplicating hits if the visitor reloads or re-visits a page:
private function has_hit($loginfo)
{
$this->db->where('ip', $loginfo->ip);
$this->db->where('page_ref', $loginfo->page_ref);
$result = $this->db->get('your_log_table_name');
if($result->num_rows() > 0)
{
return TRUE;
} else {
return FALSE;
}
A MySql schema is provided in the Loginfo.php file for the log table
Here is an example of the Loginfo class output. On first visit, the location cookie won't be set, to simulate this you could try disabling cookies and reloading the page. In that case, the IP location lookup will be done at ipinfodb.com. Once cookies are re-enabled, the location cookie is set and used for location information. this improves your applications responsiveness, and reduces loads on ipinfodb.com:
Here is an example from my log table (sorry, you have to make your own table)
| Hit | IP | R | Browser | Location | Lat | Long | Page | Date |
|---|---|---|---|---|---|---|---|---|
| 547 | 94.23.145.229 | ? | MSIE | Rotterdam Zuid-Holland NL | 51.9167 | 4.5 | home | 2010-04-03 10:27:55 |
| 546 | 77.88.30.246 | ? | Other | Moscow Moscow City RU | 55.7522 | 37.6156 | home | 2010-04-03 10:22:40 |
| 545 | 174.115.140.241 | ? | Firefox | Newmarket Ontario CA | 44.05 | -79.45 | 38 | 2010-04-03 10:01:02 |
| 544 | 91.212.226.42 | ? | Opera | Zhirkov Volgograd RU | 48.6458 | 42.9181 | home | 2010-04-03 06:18:56 |
| 543 | 41.203.222.74 | ? | Firefox | Nairobi Nairobi Area KE | -1.2833 | 36.8167 | home | 2010-04-03 05:40:19 |
| 542 | 77.88.30.246 | ? | Other | Moscow Moscow City RU | 55.7522 | 37.6156 | 37 | 2010-04-03 04:35:52 |
| 541 | 66.249.65.15 | ? | Cabot Arkansas US | 34.9461 | -92.0858 | 34 | 2010-04-03 03:29:16 | |
| 540 | 76.227.153.189 | ? | Safari | Santa Rosa California US | 38.4493 | -122.709 | home | 2010-04-03 03:18:50 |
| 539 | 220.181.7.17 | ? | Other | Beijing Beijing CN | 39.9289 | 116.388 | home | 2010-04-03 02:37:39 |
| 538 | 122.160.99.22 | ? | Other | Chandigarh Chandigarh IN | 30.7372 | 76.7872 | home | 2010-04-03 02:17:53 |
| 537 | 213.191.7.145 | ? | MSIE | Yekaterinburg Sverdlovsk RU | 56.85 | 60.6 | home | 2010-04-03 01:12:28 |
| 536 | 208.115.111.244 | ? | Other | Seattle Washington US | 47.6145 | -122.348 | home | 2010-04-03 00:58:26 |
| 535 | 60.50.143.23 | ? | Safari | Johor Bahru Johor MY | 1.4667 | 103.75 | home | 2010-04-03 00:37:13 |
| 534 | 71.191.150.174 | ? | Safari | Fairfax Virginia US | 38.8605 | -77.263 | home | 2010-04-03 00:30:49 |
| 533 | 174.115.140.241 | ? | Other | Newmarket Ontario CA | 44.05 | -79.45 | rssfeed | 2010-04-02 23:58:13 |
You should visit ipinfodb.com for their IP location terms of service
Visit techpatterns.com for alternative browser detection
?

Comment by Bryan, Mon Apr 26 2010, 09:36 PM