Skip to content

Instantly share code, notes, and snippets.

@psaia
Created June 23, 2011 17:10
Show Gist options
  • Save psaia/1043013 to your computer and use it in GitHub Desktop.
Save psaia/1043013 to your computer and use it in GitHub Desktop.
Geolocation
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
-------------------------------
Author: Pete Saia
Certifikid.com
*/
class User_location
{
function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('session');
$this->ci->load->library('input');
$this->ci->load->model('area_model');
}
/*
-------------------------------
This library sets the user_data
session variable and returns
the appropriate city slug.
-------------------------------
*/
function get_area()
{
/*
-------------------------------
if session is set...
*/
//$this->ci->load->library('firephp');
//$this->ci->firephp->log(get_geolocation($this->ci->input->ip_address()));
//TEMP FIX (theres something wrong with brian's computer in the office (IP issues i think))
$this->ci->session->set_userdata(array('user_area' => 'washington-dc'));
return 'washington-dc';
//TEMP FIX
if ($this->ci->session->userdata('user_area'))
{
return $this->ci->session->userdata('user_area');
}
else
{
/*
-------------------------------
Get info based on IP
*/
//$data = get_geolocation('69.140.43.109'); //greenbelt
//$data = get_geolocation('71.251.51.248'); //dc
$data = get_geolocation($this->ci->input->ip_address()); //dc
/*
-------------------------------
If somethingis messed up with the
IP address just pick up frst city...
*/
if (strlen($data['city'])<1 || strlen($data['region_name'])<1)
{
$this->ci->session->set_userdata(array('user_area' => $this->ci->area_model->first_city()));
return $this->ci->area_model->first_city();
}
/*
-------------------------------
Check to see if the user is in
a Featured area
*/
$query = $this->ci->area_model->is_in_featured_area($data['city'],$data['region_name']);
//$this->ci->firephp->log($query->row());
//TEMPORARY FIX FOR CLIENTS COMPUTER
if ($query->num_rows()>0)
{
$city = $query->row();
$this->ci->session->set_userdata(array('user_area' => $city->slug));
return $city->slug;
}
/*
-------------------------------
if there not, check to see if
they are in a surrounding area...
*/
else
{
$query = $this->ci->area_model->surrounding_areas();
foreach($query->result_array() as $row) //see if user's current
{
if (strtolower($data['city'])==strtolower($row['name']) || strtolower($data['region_name'])==strtolower($row['name']))
{
/*
-------------------------------
They are, so get the slug for that
surrounding area's parent area.
*/
$parent_city_ID = $row['locations_id'];
$city = $this->ci->area_model->return_parent_area($parent_city_ID);
$this->ci->session->set_userdata(array('user_area' => $city));
return $city;
}
}
/*
-------------------------------
else there somewhere else so just
return first area in database
*/
$this->ci->session->set_userdata(array('user_area' => $this->ci->area_model->first_city()));
return $this->ci->area_model->first_city();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment