LOCATION BASED ADVERTISEMENTS

At its most basic, an agent on a website has an IP address. By using a database, then known ip address ranges can narrow down the search. We have seen many problems with mixed http and https configurations.

OBTAIN THE CLIENT IP ADDRESS

$protocol 	= "http://";
$useraddress =  $_SERVER[‘REMOTE_ADDR’];
if ($_SERVER['HTTPS'] == 'on') $protocol = "https://";
 
$domain	= "linux-guru.azurewebsites.net"; // identify the site domain from settings
define('WP_DOMAIN', $domain); 
define('WP_HOME', "{$protocol}{$domain}"); 
define('WP_SITEURL', "{$protocol}{$domain}");

we are aware of free services to locate a user by IP address, we have however created our own database to determine the country the visitor is from.

CONNECT TO THE DATABASE

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "ip2location";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT country_code FROM ip2location_db1 WHERE " + $useraddress + " BETWEEN ip_from AND ip_to"; 
$result = $conn->query($sql);

We use a separate database to store the location table, this makes sense with multiple websites. With a 2 letter location code we can use a simple PHP switch() statement to display relevant advertisements, which means more clicks and more earning!

SELECT THE SUITABLE AD

switch ($result) {
    case: "us": echo "US ads"; break;
    case: "uk": echo "UK ads"; break;
    case: "ca": echo "CA ads"; break;
    default: echo "US ads";
}

By using a location database makes it easier to effectively monetize a website. Depending on the affiliate plan, some offer location sensitive ads already but many others do not. Using a database for managing ads moves a developer into a new realm;

SQL SELECT

SELECT code FROM adverts WHERE
        begins <= NOW AND NOW() <= expires AND
        (impressions_max IS NULL OR impressions < impressions_max) AND
        (views_max IS NULL OR views < views_max)

using a database of adverts makes sense when there is a large inventory