Proxies, Free Proxies, Proxy Servers, CGIProxy, PHProxy, Proxy Sites, Proxy List and New Proxy Sites by ProxySphere
Proxies, Free Proxies, Proxy Servers, CGIProxy, PHProxy, Proxy Sites, Proxy List, and New Proxies by ProxySphere Link Auction Software
Home Proxies Top Proxies Articles Community Proxy KB CGIProxy PHProxy

PHProxy Statistics and Abuse Prevention with PHP and MySQL

This article contains a PHProxy hack that has been implemented which tracks usage with MySQL, outputs nice formatted stats, and employs IP limiting to drastically reduce bandwidth consumption and abuse.

This logs everything - unique URLs, IPs, and domains. I wrote this PHProxy logger which uses MySQL and also has a custom reporting page, as well as per-IP traffic limits. This was built on PHProxy class updated 9/8/2005. Make sure you replace all instances of www.myproxysite.com with your domain name.

Step 1: Setup MySQL Database

Create a MySQL database and user with INSERT/UPDATE privileges. Run the following SQL code to create the table structures:

CREATE TABLE tbl_ips (
  ip varchar(15) NOT NULL default '',
  hits_current int(11) unsigned NOT NULL default '0',
  hits_attempts int(11) unsigned NOT NULL default '0',
  hits_total int(11) unsigned NOT NULL default '0',
  last_url varchar(255) NOT NULL default '',
  updated datetime NOT NULL default '0000-00-00 00:00:00'
) TYPE=MyISAM;

CREATE TABLE tbl_sites (
  site_id int(11) unsigned NOT NULL auto_increment,
  site_domain varchar(255) NOT NULL default '',
  site_count int(11) unsigned NOT NULL default '0',
  site_lasturl varchar(255) NOT NULL default '',
  PRIMARY KEY  (site_id),
  KEY site_domain (site_domain)
) TYPE=MyISAM AUTO_INCREMENT=6204 ;


CREATE TABLE tbl_urls (
  url_id int(11) unsigned NOT NULL auto_increment,
  site_id int(11) unsigned NOT NULL default '0',
  url_url varchar(255) NOT NULL default '',
  url_count int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (url_id),
  KEY site_id (site_id)
) TYPE=MyISAM AUTO_INCREMENT=6204 ;

Step 2: Modify PHProxy.class.php

Add the following code to PHProxy.class.php before "class PHProxy", replacing the DB_HOST, DB_USERNAME, DB_PASSWORD, and DB_DATABASE fields with the appropriate database details.

define('IP_LIFESPAN', 3600);
define('IP_LIMIT', 5);
define('NOTICE_LIFESPAN', 300);
define('NOTICE_LIMIT', 20);
define('NOTICE_RECIPIENT', 'email@yourdomain.com');
$link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE, $link);

function dbtime_to_unix($datetime)
{
	return strtotime($datetime);
}

function unix_to_dbtime($timestamp)
{
	return date('Y-m-d H:i:s', $timestamp);
}

function str_truncate($string, $max, $suffix = '...')
{
    if(strlen($string) > $max)
    {
        $string = substr($string,0,$max);
        $string .= $suffix;
    }
    return $string;
}

Add the following code to PHProxy.class.php directly after "case: 'body':" and before "if ($this->flags['include_form'])"

$map = parse_url($this->url);
                    $domain = $map['host'];
                    if(empty($domain)) $domain = 'Invalid URL';
                
                    $sql = 'SELECT site_id, site_count FROM tbl_sites WHERE site_domain = "' . $domain . '"';
                    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
                    {
                        $row = mysql_fetch_assoc($result);
                        $count = $row['site_count'] + 1;
                        $sql = 'UPDATE tbl_sites SET site_count = ' . $count . ' WHERE site_id = ' . $row['site_id'];
                        
                        mysql_query($sql);
                        unset($result);
                        
                        $sql = 'SELECT url_id, url_count FROM tbl_urls WHERE url_url = "' . addslashes($this->url) . '"';
                
                        if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
                        {
                            $urow = mysql_fetch_assoc($result);
                            $count = $urow['url_count'] + 1;
                            $sql = 'UPDATE tbl_urls SET url_count = ' . $count . ' WHERE url_id = ' . $urow['url_id'];
            
                            mysql_query($sql);
                        }
                        else 
                        {
                            $sql = 'INSERT INTO tbl_urls (site_id,url_url,url_count) VALUES(' . $row['site_id'] . ',"' . addslashes($this->url) . '",1';
                            mysql_query($sql);
                        }
                    }
                    else 
                    {
                        $sql = 'INSERT INTO tbl_sites (site_domain,site_count) VALUES("' . $domain . '",1)';
                        mysql_query($sql);
                        
                        $last_id = mysql_insert_id();
                        
                        $sql = 'INSERT INTO tbl_urls (site_id,url_url,url_count) VALUES(' . $last_id . ',"' . addslashes($this->url) . '",1)';
                        mysql_query($sql);
                        
                    }       
                    $client_ip = $this->get_ip();
                    
                    $sql = 'SELECT ip, hits_current, hits_total, updated FROM tbl_ips WHERE ip = "' . $client_ip . '"';           
                    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
                    {
                        $row = mysql_fetch_assoc($result);
                        // Has last updated time limit been exceeded?
                        if(((strtotime($row['updated']) + IP_LIFESPAN) < time()) && ($row['hits_current'] > 1))
                        {
                            $sql = 'UPDATE tbl_ips (hits_current,hits_total,hits_attempts,updated) VALUES(1,' . $row['hits_total'] + $row['hits_current'] . ',' . $row['hits_total'] + $row['hits_current'] . '"' . unix_to_dbtime(time()) . '")';
                            mysql_query($sql);
                        }
                        else
                        {
                            // Has IP reached maximum for the period?
                            if($row['hits_current'] < IP_LIMIT)
                            {
                                $new_hits = $row["hits_current"] + 1;
                                $sql = 'UPDATE tbl_ips SET hits_current = ' . $new_hits . ', hits_attempts = ' . $new_hits . ', last_url = "' . $this->url . '" WHERE ip = "' . $client_ip . '"';
            
                              mysql_query($sql);
                            }
                            else
                            {
                                $sql = 'UPDATE tbl_ips SET hits_attempts = . ' . $new_hits . ', last_url = "' . $this->url . '" WHERE ip = "' . $client_ip . '"';
                              mysql_query($sql);
                                // IP has reached the limit for this time period.
                                $limit_page = '<html><head><meta http-equiv="refresh" content="4;url=http://www.myproxysite.com/?error=Request%20limit%20has%20been%20reached%20for%20this%20IP%20address. The%20administrator%20has%20set%20a%20limit%20of%20' . IP_LIMIT . '%20requests%20every%20' . IP_LIFESPAN/60 . '%20minutes."><link rel="stylesheet" type="text/css" href="/style.css" /></head><body>' .
                                              '<h1>Limit Exceeded for ' . $client_ip . '</h1>' . 
                                              '<p>The administrator has set a limit of ' . IP_LIMIT . ' requests every ' . IP_LIFESPAN/60 . ' minutes. This measure is here to prevent abuse of this free system, and we apologize for any inconvenience.</p>' . 
                                              '<p>Check out some other great proxies at <a href="http://www.cyberwoot.com">CyberWoot! Top Sites</a>.</p>' . 
                                              '<p>You will be forwarded to the home page briefly.</p>' .
                                              '<script type="text/javascript" src="http://www.servermind.com/smp.js"></script>';
                                die($limit_page); 
                            }
                        }            
                    }
                    else 
                    {
                        $sql = 'INSERT INTO tbl_ips (ip,hits_current,hits_total,last_url,updated) VALUES("' . $client_ip . '",1,1,"' . $this->url . '","' . unix_to_dbtime(time()) . '")';
            
                        mysql_query($sql);
                    }

Step 3: Create reporting script

Create a new file (I called it report.php) and insert the following code:

<?php
$link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE, $link);

function dbtime_to_unix($datetime)
{
	return strtotime($datetime);
}

function unix_to_dbtime($timestamp)
{
	return date('Y-m-d H:i:s', $timestamp);
}

function str_truncate($string, $max, $suffix = '...')
{
    if(strlen($string) > $max)
    {
        $string = substr($string,0,$max);
        $string .= $suffix;
    }
    return $string;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Proxy Report</title>
    <link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body>

<?php
echo '<h2>' . SITE_TITLE . ' Stats</h2>';

if($_GET['a'] == 'sites')
{
    $sql = 'SELECT site_id, site_count FROM tbl_sites WHERE site_domain = "' . $domain . '"';
}
else
{
    // Show summary stats
    
    $sql = 'SELECT COUNT(site_id) AS sites_total, SUM(site_count) as sites_total_hits FROM tbl_sites';
    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
    {
        $row = mysql_fetch_assoc($result);
        $sites_total = $row['sites_total'];
        $sites_total_hits = $row['sites_total_hits'];
    }

    $sql = 'SELECT COUNT(url_id) AS urls_total, SUM(url_count) as urls_total_hits FROM tbl_urls';
    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
    {
        $row = mysql_fetch_assoc($result);
        $urls_total = $row['urls_total'];
        $urls_total_hits = $row['urls_total_hits'];
    }
    $sql = 'SELECT COUNT(ip) AS ips_total, SUM(hits_current) as ips_hits_current, SUM(hits_attempts) AS ips_hits_attempts, SUM(hits_total) AS ips_hits_total FROM tbl_ips';
    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
    {
        $row = mysql_fetch_assoc($result);
        $ips_total = $row['ips_total'];
        $ips_hits_current = $row['ips_hits_current'];
        $ips_hits_attempts = $row['ips_hits_attempts'];
        $ips_hits_total = $row['ips_hits_total'];
    }

    echo '<table cellpadding="4px" cellspacing="0px" border="0px" class="rpt-table">';
    echo '<tr><td class="rpt-header" colspan="2">Site Stats (<a href="#sites">detailed</a>)</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Requested Domains:</td><td width="100%" align="left">' . $sites_total . '</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total Site Requests:</td><td align="left">' . $sites_total_hits . '</td></tr>';
    echo '<tr><td class="rpt-header" colspan="2">URL Stats</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total URLs Visited:</td><td align="left">' . $urls_total . '</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total URL Hits:</td><td align="left">' . $urls_total_hits . '</td></tr>';
    echo '<tr><td class="rpt-header" colspan="2">IP Stats (<a href="#ips">detailed</a>)</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total IPs:</td><td align="left">' . $ips_total . '</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Current IP Hits:</td><td align="left">' . $ips_hits_current . '</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total IP Attempts:</td><td align="left">' . $ips_hits_attempts . '</td></tr>';
    echo '<tr><td nowrap class="rpt-field">Total IP Hits:</td><td align="left">' . $ips_hits_total . '</td></tr>';
    echo '</table><br /><br />';
    
    $sql = 'SELECT tbl_sites.site_id, site_domain, site_count, site_lasturl FROM tbl_sites ORDER BY site_count DESC';

    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
    {
        echo '<a name="sites"></a>';
        echo '<table width="100%" cellpadding="4px" cellspacing="0px" border="0px" class="rpt-table">';
        echo '<tr><td class="rpt-header" colspan="3">ADVANCED SITE STATS</td></tr>';
        echo '<tr><td class="rpt-field">Domain</td><td class="rpt-field">Count</td><td class="rpt-field">Last URL</td></tr>';
        while($row = mysql_fetch_assoc($result))
        {
            echo '<tr><td><a href="#" title="' . $row['site_domain'] . '">' . str_truncate($row['site_domain'], 60) . '</a></td><td>' . $row['site_count'] . '</td><td><a href="' . $row['site_lasturl'] . '" target="_external" title="' . $row['site_lasturl'] . '">' . str_truncate($row['site_lasturl'], 50) . '</a></td></tr>';
        }
        echo '</table><br /><br />';
    }
    
    $sql = 'SELECT tbl_ips.ip, hits_current, hits_attempts, hits_total, last_url, updated FROM tbl_ips ORDER BY hits_attempts DESC';

    if(($result = mysql_query($sql)) && (mysql_num_rows($result) !== 0))
    {
        echo '<a name="ips"></a>';
        echo '<table width="100%" cellpadding="4px" cellspacing="0px" border="0px" class="rpt-table">';
        echo '<tr><td class="rpt-header" colspan="6">ADVANCED IP STATS</td></tr>';
        echo '<tr><td class="rpt-field">IP Address</td><td class="rpt-field">Current Count</td><td class="rpt-field">Total Count</td><td class="rpt-field">Current Attempts</td><td class="rpt-field">Last URL</td><td nowrap class="rpt-field">Updated</td></tr>';
        while($row = mysql_fetch_assoc($result))
        {
            echo '<tr><td>' . $row['ip'] . '</td><td>' . $row['hits_current'] . '</td><td>' . $row['hits_total'] . '</td><td>' . $row['hits_attempts'] . '</td><td><a href="' . $row['last_url'] . '" target="_external" title="' . $row['last_url'] . '">' . str_truncate($row['last_url'], 50) . '</a></td><td nowrap>' . $row['updated'] . '</td></tr>';
        }
        echo '</table>';
    }
}
?>
</body>
</html>

boy million
ProxySphere is the place for proxies, free proxies, proxy forums, proxy servers, CGIProxy, PHProxy, working proxy sites,
info and much, much more! We focus on proxy information and resources for proxy users and proxy owners.