<?php
#############################################################
# Project:      bCounter                                    #
# File:         counter.php                                 #
# Decription:   include this file to collect data
#                                                           #
# Copyright (c) Alexander Bondar (http://bondar.de/)        #
#############################################################
    global $bCounter_inc_dir;
    $bCounter_inc_dir = dirname(__FILE__)."/inc/";

    @require_once($bCounter_inc_dir."inc.php");

    if ( !isset($_COOKIE['bCounter_ignore_me']) && !(isset($bCounter_ignore_this_page) && $bCounter_ignore_this_page == true ) ) {
        $iTimestamp = time();
        $strHost = @gethostbyaddr(mysql_real_escape_string($_SERVER["REMOTE_ADDR"]));
        $strIP = "";
        $strIP_regExp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"; # regular expression to check the IP address

        if ( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) && eregi( $strIP_regExp, $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
            $strIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        elseif ( isset( $_SERVER["HTTP_CLIENT_IP"] ) && eregi( $strIP_regExp, $_SERVER["HTTP_CLIENT_IP"] ) ) {
            $strIP = $_SERVER["HTTP_CLIENT_IP"];
        }
        else {
            $strIP = $_SERVER["REMOTE_ADDR"];
        }
        $strIP = @mysql_real_escape_string($strIP);
        $strSQLstring =
"INSERT INTO ".$bCounter_dbPrefix."data(redirect_status, user_agent, remote_addr, host, redirect_url, request_uri, timestamp, referer)
VALUES(
'".@mysql_real_escape_string($_SERVER["REDIRECT_STATUS"])."',
'".@mysql_real_escape_string($_SERVER["HTTP_USER_AGENT"])."',
'".$strIP."',
'$strHost',
'".@mysql_real_escape_string($_SERVER["REDIRECT_URL"])."',
'".@mysql_real_escape_string($_SERVER["REQUEST_URI"])."',
'".$iTimestamp."',
'".@mysql_real_escape_string($_SERVER["HTTP_REFERER"])."'
)";
        $bCounterDB->query($strSQLstring);
        @updateVisits( $strIP, $strHost, $iTimestamp, mysql_real_escape_string($_SERVER["HTTP_USER_AGENT"]), mysql_real_escape_string($_SERVER["HTTP_REFERER"]) );
    }
?>