Save your website bandwidth with PHProxy and ip2nation
What is ip2nation?

There is currently numerous companies on the net charging for databases or files containing knowledge about where an IP is apportioned (as in which country). This knowledge is in fact obtainable free at ARIN, APNIC, RIPE etcetera. Though, those files are not in any way optimized for queries & are indeed very slow. This is where ip2nation.com comes in.

We utilitize these files to generate a MySQL database table with the appropriate indexes. We have also optimized the table & removed unneccesary information in order to make queries lightning speedy. In order to utilitize the table you may find help in the PHP examples under “Sample scripts”.

What is PHPProxy?

PHProxy is a web HTTP proxy programmed in PHP meant to bypass firewalls and access otherwise inaccessible resources (i.e. blocked sites). If the server this script is run on can access a resource, so are you able to!

With the proxy script PHProxy and the information IP location database from ip2nation it is simple to block visitors which are coming from other countries then your target group. In this tutorial you will learn how to add the necessary code to your PHProxy powered proxy web-site.

Step 1

Download ip2nation and import the sql file into your database (test).

Step 2

Download PHPProxy and add the following code to the index.php file

$DBHOST = 'localhost';
$DBUSER = 'user';
$DBPASS = 'password';
$DATABASE = 'test';
$my_countries = array('in', 'us');
$use_ip2nation = true; //true for blocking countries

Step 3

Find the following code in index.php

function proxify_css_url($url)
{
$url = trim($url);
$delim = strpos($url, ‘”‘) === 0 ? ‘”‘ : (strpos($url, “‘”) === 0 ? “‘” : ”);

return $delim . preg_replace(‘#([\(\),\s\'”\\\])#’, ‘\\$1’, complete_url(trim(preg_replace(‘#\\\(.)#’, ‘$1’, trim($url, $delim))))) . $delim;
}
Add the following code after that to check if the resulting country is in our list or not. if its in our list it will go ahead.

if ($use_ip2nation) {
$db = mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DATABASE);
$sql = "SELECT country FROM ip2nation WHERE ip < INET_ATON('".$_SERVER['REMOTE_ADDR']."') ORDER BY ip DESC LIMIT 0,1";
$res = mysql_query($sql);
$country = mysql_result($res, 0, 'country');
mysql_free_result($res);
if (!in_array($country, $my_countries)) {
show_report(array('which' => 'index', 'category' => 'error', 'group' => 'resource', 'type' => 'otherthan_in_us'));
}
}

Step 4

Add the following code in index.inc.php.

$show_web_form = true;

Find the following code index.inc.php (near line 83)

case ‘hotlinking’:
$message = ‘It appears that you are trying to access a resource through this proxy from a remote Website.<br />’
. ‘For security reasons, please use the form below to do so.’;
break;

Add the following code after that

case 'otherthan_in_us':
$message = 'Sorry this service is only for india and us.
Click topsite in the footer part.';
$show_web_form = false;
break;

Add the following code to allow user to view form or not before starting of <form> tag in index.inc.php

if ($show_web_form) {

and add this code after </form> tag to show alternate stuff to user.

} else {
// show custom content for blocked user to tell that they are blocked for the content
};

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s