Get exclusive CAP network offers from top brands

View CAP Offers

Reply To: anyone have a geotargeting

[bsa_pro_ad_space id=2]
#709169
Anonymous
Inactive

yes

But it goes through database and is in php…

/// This is what you have to change, the location where it must be redirected
$location=”http://www.whateverdomain.com”;

//This is the US country code, you can change it if you want to test it for another country
$code=”US”;

define(‘IP2LOCATION_DB_HOST’, ‘localhost’);
define(‘IP2LOCATION_DB_USER’, ‘ip2location’);
define(‘IP2LOCATION_DB_PASS’, ‘ip2123+’);
define(‘IP2LOCATION_DB_NAME’, ‘ip2location’);
define(‘IP2LOCATION_DB_TABLE’, ‘IPREGIONCITYISP’);

redirect($location, $code);

function redirect($location,$code){
$info=Geo::getCurrentLocation();
if($info==$code){
header(“Location: $location”);
}
}

function encode($s)
{
$s = str_replace(‘\’, ‘\\’, $s);
return str_replace(“‘”, “\'”, $s);
}

function execSQL($sql)
{

global $DEFAULT_CONNECTION;

$r = @mysql_query($sql, $DEFAULT_CONNECTION);
if (!$r)
{
$err = mysql_error();
print_r(debug_backtrace());
die($err . ‘

‘. $sql); // salvam vre-ul log cu sql si arr
}
return $r;
}

class Query
{
var $sql = null;
var $r = null;
var $a = null;

function Query($_sql = ”)
{
$this->sql = $_sql;
}

function execute() // for internal use
{
$this->r = execSQL($this->sql);
return $this->r;
}

//function &fetch()
function fetch()
{
if (!$this->r) $this->execute();
$this->a = mysql_fetch_array($this->r, MYSQL_ASSOC);
return $this->a;
}

function getCount()
{
if (!$this->r) $this->execute();
return mysql_num_rows($this->r);
}

function getArray($count = 0, $offset = 0)
{
if ($count > 0)
{
$osql = $this->sql;
$this->sql .= ” limit $offset, “. ($offset+$count);
}

$this->execute();
$this->a = false;
$res = array();
$i = 0;
while ($a = $this->fetch())
{
if ($i < $lim_low) { $i++; continue; }
if ($count > 0) if ($i >= $lim_low + $count) break;
$i++;
$res[] = $a;
}

if (count > 0)
{
$this->sql = $osql;
}

return $res;
}

function getArrayByID($id_field = “ID”)
{
$this->execute();
$this->a = false;
$res = array();
while ($a = $this->fetch()) $res[$a[$id_field]] = $a;
return $res;
}

function getAssocArray($key, $value)
{
$this->execute();
$this->a = false;
$res = array();
while ($a = $this->fetch()) $res[$a[$key]] = $a[$value];
return $res;
}

function getColumn($column, $index=””)
{
$this->execute();
$this->a = false;
$res = array();
while ($a = $this->fetch())
if ($index)
$res[$a[$index]] = $a[$column];
else
$res[] = $a[$column];
return $res;
}

function getScalar($column)
{
$this->execute();
$this->a = $this->fetch();
return $this->a[$column];
}
}

class Table
{
var $table_name;
var $keys;
var $fields;
var $auto_increment;

function read($key_values) // assoc_array
{
if (count($this->keys) > 0); else die(‘No keys defined.’);

$where = ”;
foreach ($this->keys as $k=>$v) $where .= ” and `$v`='”.encode($key_values[$v]).”‘”;
$where = substr($where, 4);

$sql = “select * from `$this->table_name` where $where limit 1”;
$r = new Query($sql);
return $r->fetch();
}

function insert($values) // assoc_array
{
if ($this->auto_increment)
unset($values[$this->keys[0]]);

foreach($this->fields as $field_name => $field)
{
if (isset($values[$field_name]))
{
$a .= ‘,`’ . $field_name . ‘`’;
$b .= ‘,” . encode($values[$field_name]) . ”’;
}
}

$a = substr($a,1);
$b = substr($b,1);

if (execSQL(“insert into `{$this->table_name}`($a) values($b)”))
{
if ($this->auto_increment)
return mysql_insert_id($GLOBALS);
else
return true;
}
else
{
return false;
}
}

function update($key_values, $values) // assoc_array
{
if (count($this->keys) > 0); else die(‘No keys defined.’);

foreach ($this->keys as $k=>$v) unset($values[$v]);

$where = ”;
foreach ($this->keys as $k=>$v) $where .= ” and `$v`='”.encode($key_values[$v]).”‘”;
$where = substr($where, 4);

foreach($this->fields as $field_name => $field)
{
if (isset($values[$field_name]))
{
$a .= ‘, `’ . $field_name . ‘`’;
$a .= ‘=” . encode($values[$field_name]) . ”’;
}
}

$a = substr($a,1);
$b = substr($b,1);

return execSQL(“update `{$this->table_name}` set $a where $where”);
}

function replace($key_values, $values) // assoc_array
{
if (count($this->keys) > 0); else die(‘No keys defined.’);

foreach ($this->keys as $k=>$v) $values[$v] = $key_values[$v];

foreach($this->fields as $field_name => $field)
{
if (isset($values[$field_name]))
{
$a .= ‘,`’ . $field_name . ‘`’;
$b .= ‘,” . encode($values[$field_name]) . ”’;
}
}

$a = substr($a,1);
$b = substr($b,1);

return execSQL(“replace into `{$this->table_name}`($a) values($b)”);
}

function delete($key_values) // assoc_array
{
if (count($this->keys) > 0); else die(‘No keys defined.’);

$where = ”;
foreach ($this->keys as $k=>$v) $where .= ” and `$v`='”.encode($key_values[$v]).”‘”;
$where = substr($where, 4);

return execSQL(“delete from `{$this->table_name}` where $where”);
}
}

class Geo
{
function getCurrentLocation(){
$ip = IP::getIP();
$conn = mysql_connect(IP2LOCATION_DB_HOST, IP2LOCATION_DB_USER, IP2LOCATION_DB_PASS, true);
mysql_select_db(IP2LOCATION_DB_NAME, $conn);
$q = mysql_query(‘select * from ‘.IP2LOCATION_DB_TABLE.’ where ipFROM<='.$ip.' order by ipFROM desc limit 1', $conn);
$a = mysql_fetch_array($q);
if ($a && $a >= $ip)
{
$a = $a;
$a = $a;
$a = $a;
$a = $ip;
return $a;
}
else return false;
}
}

class IP
{
function isValid($ip_addr)
{
$rr = array();
if (!ereg(‘^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$’, $ip_addr, $rr)) return false;

return true
&& ($rr[1] >= 0) && ($rr[1] <= 255)
&& ($rr[2] >= 0) && ($rr[2] <= 255)
&& ($rr[3] >= 0) && ($rr[3] <= 255)
&& ($rr[4] >= 0) && ($rr[4] <= 255);
}

function dotToLongIP ($IPaddr)
{
if ($IPaddr == “”)
{
return 0;
}
else
{
$ips = split (“.”, “$IPaddr”);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] * 16777216);
}
}

function longIPToDot($ip, $zero_padding = false)
{
$w = ( $ip / 16777216 ) % 256;
$x = ( $ip / 65536 ) % 256;
$y = ( $ip / 256 ) % 256;
$z = ( $ip ) % 256;
if ($ip>2147483648)
{
$ipt=$ip-2147483648;
$z=$ipt%256;
}

if ($zero_padding)
return sprintf(“%03d.%03d.%03d.%03d”, $w,$x,$y,$z);
else
return “$w.$x.$y.$z”;
}

function getDotIP()
{
if (getenv(“HTTP_CLIENT_IP”) && strcasecmp(getenv(“HTTP_CLIENT_IP”), “unknown”))
$ip = getenv(“HTTP_CLIENT_IP”);
else if (getenv(“HTTP_X_FORWARDED_FOR”) && strcasecmp(getenv(“HTTP_X_FORWARDED_FOR”), “unknown”))
$ip = getenv(“HTTP_X_FORWARDED_FOR”);
else if (getenv(“REMOTE_ADDR”) && strcasecmp(getenv(“REMOTE_ADDR”), “unknown”))
$ip = getenv(“REMOTE_ADDR”);
else if (isset($_SERVER) && $_SERVER && strcasecmp($_SERVER, “unknown”))
$ip = $_SERVER;
else
$ip = “”;
return($ip);
}

function getIP()
{
return IP::dotToLongIP( IP::getDotIP() );
}
}

?>