Save bandwidth with PHProxy and ip2nation

If your website becomes a little busy the websites bandwidth becomes very valuable. If your visitors using too much bandwidth and the conversions from your website are very low or just moderate, it’s possible that you get broke with also a good Internet website project. During the last month the web proxy business was a very hot item with thousands of new web proxies every day. Since it’s not allowed to place Google ads on the “proxified” pages, it’s very difficult to find alternatives which pay enough that you can pay your hosting bills. One of them is AdVersal, they pay a moderate amount for traffic generated by US, UK or CA visitors. But what if your traffic and bandwidth is mostly used by visitors from other countries? In this case the visitors using your server resources and visitors from your target group are maybe not able to use one of your fast web proxies.

With the proxy script PHProxy (version 0.5b2 from 20th January 2007) and the data IP location database from ip2nation it’s very easy to block visitors which are coming from other countries then your target group. In this tutorial you will learn how to add the required code to your PHProxy powered proxy website.

First we need to prepare the MySQL database, download and install the ip2nation database table dump. Use phpMyAdmin to import the database tables inclusive the data.

The PHProxy script comes with two php files index.php and index.inc.php, first we need to add the following code to the index.php (script) file:

$DBHOST = '';
$DBUSER = '';
$DBPASS = '';
$DATABASE = '';
$my_countries = array('us', 'ca', 'gb');
$use_ip2nation = true;

The first four variables are used for the database connection, the array indicates the countries you want to allow access to your proxy and the last Boolean variable is used to switch the country block on or off (this way you don’t need to remove all code).

Next we need to add this code just below the function proxify_css_url (must be around row 325):

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' =>; 'non_us'));
	}
}

This code will query the database and compares the IP address from the client with the collected data. If the resulting country code exists in your list of country codes the script will continue and otherwise an error (code “non_us”) is stored in the error message array.

OK, next we need to add some code to the index.inc.php (template) file. At the top of the file, we need to add this variable to tell script to show the web form:

$show_web_form = true;

Next locate the PHP code part with all the error message code and add this error information below the break for the message which is defined for “hot-linking”:

case 'non_us':
$message = 'We sorry, our service is currently not available.
Please try a different proxy via the top-site buttons in the websites footer.';
$show_web_form = false;
break;

Together with this message the Boolean to hide the web form is set to “false”. This part is actually not required for blocking visitors, but makes the whole thing more complete. Around the form we need to create a condition which will show the form or not, add before the form openings tag this code (if):

if ($show_web_form) {

And below the form ending tag this alternative code (else):

} else {
	// place here some alternative content
}

Add some ad or banner in the html which will show up if the resulting country code is not in the country (white) list. Because this function is called with every execution and if the error code exists it is not possible for visitors from “not allowed” country to view a “proxified” page.

Comments

No comments yet.

Because of all the spam attemps I've decided to close the comment form at this time. If you have have any questions or comments please post them by using Google+ or Twitter (the links to my profiles are located at the top of this page).