Location: PHPKode > projects > PHP Counter > phpcounter.1.3.2/inc.php
<?
//////////////////////////////////////////////////////////////////////
//
// PHPcounter - a web counter tracking system in PHP
//
// Copyright (C) 2004-2008 - Tsvetozar Drambozov <hide@address.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// 10 Dec 2004 - Initial version.
// 12 Dec 2004 - Table titles.
// 15 Dec 2004 - Small improvements.
//
//////////////////////////////////////////////////////////////////////
//
// Rumen Avramov <hide@address.com>
//      HTML Validator compatibility added.
//      Counter Code/JavaScript rewritten.
//
//////////////////////////////////////////////////////////////////////

include("defs.php");

$version = "1.3.2";

$font = "<font face=\"$font_name\" size=\"$font_size\">";
$font_header = "<font face=\"$font_name\" size=\"$font_size\" color=\"$font_color_header\">";

mysql_connect($dbhost,$dbuser,$dbpass) or die("Could not connect to mysql: $dbuser@$dbhost");
mysql_select_db($dbname) or die("Could not change to db: $dbname");

if(substr($counter_url,-1)!="/") $counter_url .= "/";

function phpcounter($name)
{
        global $Referer, $LocalPage;

        $rs = mysql_query("select * from phpcounters where name='$name'");
        $phpcounter = mysql_fetch_array($rs);
        if(!$phpcounter) die("Bad counter name.");

        $phpcounter_id = $phpcounter["phpcounter_id"];
        $YY = date("Y-00-00");
        $YYMM = date("Y-m-00");
        $YYMMDD = date("Y-m-d");
        $YYMMDDHHMMSS = date("Y-m-d H:i:s");
        $HH = date("H");
        $WD = date("w");
        $unique_inc = 0;
        $host = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
        if($host==$_SERVER["REMOTE_ADDR"]) $host = "";
        $tld = strtolower(substr(strrchr($host,"."),1));
        $array = parse_url($Referer);
        $referer_domain = strtolower($array["host"]);
        $array = parse_url($LocalPage);
        $local_domain = strtolower($array["host"]);
        $local_page = $LocalPage;

        $rs = mysql_query("select * from phpcounter_hits where phpcounter_id='$phpcounter_id' and ip='$_SERVER[REMOTE_ADDR]' and date>='$YYMMDD 00:00:00' and date<='$YYMMDD 23:59:59'");
        $hit = mysql_fetch_array($rs);
        if(!$hit)
        {
                $unique_inc = 1;
                mysql_query("insert into phpcounter_hits (phpcounter_id, date, ip, host, user_agent, referer, language)
                             values ('$phpcounter_id', '$YYMMDDHHMMSS', '$_SERVER[REMOTE_ADDR]', '$host', '$_SERVER[HTTP_USER_AGENT]', '$Referer', '$_SERVER[HTTP_ACCEPT_LANGUAGE]')");
                $phpcounter_hit_id = mysql_insert_id();
        }
        else $phpcounter_hit_id = $hit["phpcounter_hit_id"];
        mysql_query("update phpcounter_hits set count=count+1 where phpcounter_hit_id='$phpcounter_hit_id'");

        mysql_query("insert into phpcounter_dates (phpcounter_id, date) values ('$phpcounter_id', '$YY')");
        mysql_query("update phpcounter_dates set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and date='$YY'");
        mysql_query("insert into phpcounter_dates (phpcounter_id, date) values ('$phpcounter_id', '$YYMM')");
        mysql_query("update phpcounter_dates set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and date='$YYMM'");
        mysql_query("insert into phpcounter_dates (phpcounter_id, date) values ('$phpcounter_id', '$YYMMDD')");
        mysql_query("update phpcounter_dates set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and date='$YYMMDD'");

        mysql_query("insert into phpcounter_countries (phpcounter_id, tld) values ('$phpcounter_id', '$tld')");
        mysql_query("update phpcounter_countries set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and tld='$tld'");

        mysql_query("insert into phpcounter_hours (phpcounter_id, `hour`) values ('$phpcounter_id', '$HH')");
        mysql_query("update phpcounter_hours set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and `hour`='$HH'");

        mysql_query("insert into phpcounter_weekdays (phpcounter_id, weekday) values ('$phpcounter_id', '$WD')");
        mysql_query("update phpcounter_weekdays set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and weekday='$WD'");

        if($Referer!="")
        {
                mysql_query("insert into phpcounter_referers (phpcounter_id, referer) values ('$phpcounter_id', '$Referer')");
                mysql_query("update phpcounter_referers set count=count+1, unique_count=unique_count+'$unique_inc', date=now() where phpcounter_id='$phpcounter_id' and referer='$Referer'");

                if($referer_domain!="")
                {
                        mysql_query("insert into phpcounter_referer_domains (phpcounter_id, domain) values ('$phpcounter_id', '$referer_domain')");
                        mysql_query("update phpcounter_referer_domains set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and domain='$referer_domain'");
                }
        }
        if($local_domain!="")
        {
                mysql_query("insert into phpcounter_local_domains (phpcounter_id, domain) values ('$phpcounter_id', '$local_domain')");
                mysql_query("update phpcounter_local_domains set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and domain='$local_domain'");
        }
        if($local_page!="")
        {
                mysql_query("insert into phpcounter_local_pages (phpcounter_id, page) values ('$phpcounter_id', '$local_page')");
                mysql_query("update phpcounter_local_pages set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and page='$local_page'");
        }

        $langs = split_langs($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
        foreach($langs as $lang)
        {
                mysql_query("insert into phpcounter_languages (phpcounter_id, lang) values ('$phpcounter_id', '$lang')");
                mysql_query("update phpcounter_languages set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and lang='$lang'");
        }
        if($_SERVER["HTTP_VIA"]!="")
        {
                mysql_query("insert into phpcounter_proxies (phpcounter_id, ip, via) values ('$phpcounter_id', '$_SERVER[REMOTE_ADDR]', '$_SERVER[HTTP_VIA]')");
                mysql_query("update phpcounter_proxies set count=count+1, unique_count=unique_count+'$unique_inc' where phpcounter_id='$phpcounter_id' and ip='$_SERVER[REMOTE_ADDR]'");
        }
}

function phpcounter_stats($name, $action, $pages=false)
{
        global $l, $strings, $color_table_header, $font, $font_header, $month_names, $bgcolor1, $bgcolor2, $page, $weekday_names;

        $rs = mysql_query("select * from phpcounters where name='$name'");
        $phpcounter = mysql_fetch_array($rs);
        if(!$phpcounter) die("Bad counter name.");
        $phpcounter_id = $phpcounter["phpcounter_id"];

        $pagesize = 30;
        $count = 0;

        if($action=="years")
        {
                phpcounter_box_begin($strings["years"]);
                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date like '%-00-00'
                                   order by date desc
                                   limit 10, 1");
                $row = mysql_fetch_array($rs);

                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date like '%-00-00'".
                                         ($pages?"":" and phpcounter_date_id>='$row[phpcounter_date_id]'").
                                   " order by date asc");
                seek($rs, $page, $pagesize);
?>
          <tr>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["year"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=substr($row["date"],0,4)?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, mysql_num_rows($rs))?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=-1"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
?>
<?
                phpcounter_box_end();
        }

        if($action=="months")
        {
                phpcounter_box_begin($strings["months"]);
                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date like '%-%-00' and date not like '%-00-00'
                                   order by date desc
                                   limit 12, 1");
                $row = mysql_fetch_array($rs);

                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date like '%-%-00' and date not like '%-00-00'".
                                         ($pages?"":" and phpcounter_date_id>='$row[phpcounter_date_id]'").
                                   " order by date asc");
                seek($rs, $page, $pagesize);
?>
          <tr>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["month"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=$month_names[(int)substr($row["date"],5,2)]?> <?=substr($row["date"],0,4)?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, mysql_num_rows($rs))?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=-1"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
?>
<?
                phpcounter_box_end();
        }

        if($action=="days")
        {
                phpcounter_box_begin($strings["days"]);
                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date not like '%-%-00' and date not like '%-00-00'
                                   order by date desc limit 30, 1");
                $row = mysql_fetch_array($rs);
                $rs = mysql_query("select * from phpcounter_dates
                                   where phpcounter_id='$phpcounter_id' and date not like '%-%-00' and date not like '%-00-00'".
                                         ($pages?"":" and phpcounter_date_id>'$row[phpcounter_date_id]'").
                                   " order by date asc");
                seek($rs, $page, $pagesize);
?>
          <tr>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["day"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=(int)substr($row["date"],8,2)?> <?=$month_names[(int)substr($row["date"],5,2)]?> <?=substr($row["date"],0,4)?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, mysql_num_rows($rs))?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=-1"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="weekdays")
        {
                phpcounter_box_begin($strings["weekdays"]);
                $rs = mysql_query("select * from phpcounter_weekdays where phpcounter_id='$phpcounter_id'
                                  order by weekday asc");
?>
          <tr>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["weekday"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $weekdays = array();
                while($row = mysql_fetch_array($rs))
                        $weekdays[$row["weekday"]] = $row;

                $highlight = false;
                for($i=0; $i<7; $i++)
                {
                        $row = $weekdays[$i];
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=$weekday_names[$i]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="hours")
        {
                phpcounter_box_begin($strings["hours"]);
                $rs = mysql_query("select * from phpcounter_hours where phpcounter_id='$phpcounter_id'
                                  order by `hour` asc");
?>
          <tr>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["hour"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $hours = array();
                while($row = mysql_fetch_array($rs))
                        $hours[$row["hour"]] = $row;

                $highlight = false;
                for($i=0; $i<24; $i++)
                {
                        $row = $hours[$i];
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=sprintf("%02d:00 - %02d:00", $i, $i+1)?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="langs")
        {
                phpcounter_box_begin($strings["languages"]);
                $rs = mysql_query("select phpcounter_languages.*, phpcounter_language_defs.name as language_name, phpcounter_language_defs.flag as language_flag
                                   from phpcounter_languages
                                   left join phpcounter_language_defs on phpcounter_languages.lang=phpcounter_language_defs.lang
                                   where phpcounter_id='$phpcounter_id'
                                   order by phpcounter_languages.unique_count desc, phpcounter_languages.count desc".
                                   ($pages?"":" limit 0, 30"));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b>&nbsp;</b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b>ID</b></font></td>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["language"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>"><?=$font?><img src="flags/<?=$row["language_flag"]!=""?$row["language_flag"]:"blank.gif"?>" width="32" height="20" border="0" alt=""></font></td>
            <td bgcolor="<?=$bgcolor?>"><?=$font?><?=$row["lang"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=$row["language_name"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                }
                if(!$pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="5" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="countries")
        {
                phpcounter_box_begin($strings["countries"]);
                $rs = mysql_query("select phpcounter_countries.*, phpcounter_country_defs.name as country_name, phpcounter_country_defs.flag as country_flag
                                   from phpcounter_countries
                                   left join phpcounter_country_defs on phpcounter_countries.tld=phpcounter_country_defs.tld
                                   where phpcounter_id='$phpcounter_id'
                                   order by phpcounter_countries.unique_count desc, phpcounter_countries.count desc".
                                   ($pages?"":" limit 0, 30"));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b>&nbsp;</b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b>TLD</b></font></td>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["country"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap align="right"><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>"><?=$font?><img src="flags/<?=$row["country_flag"]!=""?$row["country_flag"]:"blank.gif"?>" width="32" height="20" border="0" alt=""></font></td>
            <td bgcolor="<?=$bgcolor?>"><?=$font?><?=$row["tld"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=$row["country_name"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                }
                if(!$pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="5" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
?>
<?
                phpcounter_box_end();
        }

        if($action=="hits")
        {
                phpcounter_box_begin($strings["hits"]);
                if($pages)
                {
                        $rs = mysql_query("select count(*) as cnt
                                           from phpcounter_hits
                                           where phpcounter_hits.phpcounter_id='$phpcounter_id'");
                        $numrows = mysql_result($rs, "cnt", 0);

                        $rs = mysql_query("select phpcounter_hits.*
                                           from phpcounter_hits
                                           where phpcounter_hits.phpcounter_id='$phpcounter_id'
                                           order by phpcounter_hits.date asc".
                                           limit($page, $pagesize));
                }
                else
                {
                        $rs = mysql_query("select phpcounter_hits.*
                                           from phpcounter_hits
                                           where phpcounter_hits.phpcounter_id='$phpcounter_id'
                                           order by phpcounter_hits.date desc
                                           limit 30, 1");
                        $row = mysql_fetch_array($rs);

                        $rs = mysql_query("select phpcounter_hits.*
                                           from phpcounter_hits
                                           where phpcounter_hits.phpcounter_id='$phpcounter_id'
                                                 and phpcounter_hit_id>='$row[phpcounter_hit_id]'
                                           order by phpcounter_hits.date asc");
                }
?>
          <tr>
          <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["date"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["hits_count"]?></b></font></td>
            <td width="100%" bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b>IP</b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["host"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["country"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["language"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;

                        $tld = strtolower(substr(strrchr($row["host"], "."),1));
                        $rs2 = mysql_query("select * from phpcounter_country_defs where tld='$tld'");
                        $country = mysql_fetch_array($rs2);

                        $langs = split_langs($row["language"]);
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["date"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap width="100%"><?=$font?><?=$row["ip"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap><?=$font?><?=$row["host"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap><?=$font?><img src="flags/<?=$country["flag"]!=""?$country["flag"]:"blank.gif"?>" width="32" height="20" border="0" alt=""></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap><?=$font?><?=$country["name"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap><?=$font?><?
            foreach($langs as $lang_full=>$lang){
            $rs2 = mysql_query("select * from phpcounter_language_defs where lang='$lang'");
            $language = mysql_fetch_array($rs2);
            ?><img src="flags/<?=$language["flag"]!=""?$language["flag"]:"empty.gif"?>" width="32" height="20" border="0" alt="<?=$lang_full?>: <?=$language["name"]?>"><img src="o.gif" width="3" height="1" border="0" alt=""><?}
            ?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="7" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="7" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=-1"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="refererdomains")
        {
                phpcounter_box_begin($strings["referer_domains"]);

                $rs = mysql_query("select count(*) as cnt
                                   from phpcounter_referer_domains
                                   where phpcounter_referer_domains.phpcounter_id='$phpcounter_id'");
                $numrows = mysql_result($rs, "cnt", 0);

                $rs = mysql_query("select phpcounter_referer_domains.*
                                   from phpcounter_referer_domains
                                   where phpcounter_referer_domains.phpcounter_id='$phpcounter_id'
                                   order by phpcounter_referer_domains.unique_count desc, phpcounter_referer_domains.count desc".
                                   limit($page, $pagesize));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["referer_domain"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap width="100%"><?=$font?><a href="<?=phpcounter_external_url($row["domain"])?>" target="_blank"><?=$row["domain"]?></a></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="referers")
        {
                phpcounter_box_begin($strings["referers"]);

                $rs = mysql_query("select count(*) as cnt
                                   from phpcounter_referers
                                   where phpcounter_referers.phpcounter_id='$phpcounter_id'");
                $numrows = mysql_result($rs, "cnt", 0);

                $rs = mysql_query("select phpcounter_referers.*
                                   from phpcounter_referers
                                   where phpcounter_referers.phpcounter_id='$phpcounter_id'
                                   order by phpcounter_referers.unique_count desc,  phpcounter_referers.count desc".
                                   limit($page, $pagesize));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["referer"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap width="100%"><?=$font?><a href="<?=phpcounter_external_url($row["referer"])?>" title="<?=$row["referer"]?>" target="_blank"><?=phpcounter_brief_text($row["referer"],50)?></a></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="localdomains")
        {
                phpcounter_box_begin($strings["local_domains"]);
                $rs = mysql_query("select count(*) as cnt
                                   from phpcounter_local_domains
                                   where phpcounter_local_domains.phpcounter_id='$phpcounter_id'");
                $numrows = mysql_result($rs, "cnt", 0);

                $rs = mysql_query("select phpcounter_local_domains.*
                                   from phpcounter_local_domains
                                   where phpcounter_local_domains.phpcounter_id='$phpcounter_id'
                                   order by phpcounter_local_domains.unique_count desc, phpcounter_local_domains.count desc".
                                   limit($page, $pagesize));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["local_domain"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap width="100%"><?=$font?><?=$row["domain"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="localpages")
        {
                phpcounter_box_begin($strings["local_pages"]);
                $rs = mysql_query("select count(*) as cnt
                                   from phpcounter_local_pages
                                   where phpcounter_local_pages.phpcounter_id='$phpcounter_id'");
                $numrows = mysql_result($rs, "cnt", 0);

                $rs = mysql_query("select phpcounter_local_pages.*
                                   from phpcounter_local_pages
                                   where phpcounter_local_pages.phpcounter_id='$phpcounter_id'
                                   order by phpcounter_local_pages.unique_count desc, phpcounter_local_pages.count desc".
                                   limit($page, $pagesize));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["local_page"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap width="100%"><?=$font?><?=$row["page"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="3" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }

        if($action=="proxies")
        {
                phpcounter_box_begin($strings["proxies"]);
                $rs = mysql_query("select count(*) as cnt
                                   from phpcounter_proxies
                                   where phpcounter_id='$phpcounter_id'");
                $numrows = mysql_result($rs, "cnt", 0);

                $rs = mysql_query("select *
                                   from phpcounter_proxies
                                   where phpcounter_id='$phpcounter_id'
                                   order by unique_count desc, count desc".
                                   limit($page, $pagesize));
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["proxy"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap width="100%"><?=$font_header?><b><?=$strings["proxy_info"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["unique"]?></b></font></td>
            <td bgcolor="<?=$color_table_header?>" nowrap><?=$font_header?><b><?=$strings["total"]?></b></font></td>
          </tr>
<?
                $highlight = false;
                while($row = mysql_fetch_array($rs))
                {
                        $bgcolor = $bgcolor1;
                        if($highlight) $bgcolor = $bgcolor2;
                        $highlight = !$highlight;
?>
          <tr>
            <td bgcolor="<?=$bgcolor?>" nowrap><?=$font?><?=$row["ip"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" width="100%"><?=$font?><?=$row["via"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["unique_count"]?></font></td>
            <td bgcolor="<?=$bgcolor?>" nowrap align="right"><?=$font?><?=$row["count"]?></font></td>
          </tr>
<?
                        $count++;
                        if($pages && $count>=$pagesize) break;
                }
                if($pages)
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="4" width="100%" align="right"><?=$font?><?phpcounter_pages_line($action, $page, $pagesize, $numrows)?></font></td>
          </tr>
<?
                }
                else
                {
?>
          <tr>
            <td bgcolor="<?=$color_table_header?>" colspan="4" width="100%" align="right"><?=$font?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0"><?=$strings["more"]?> &gt;&gt;</a></font></td>
          </tr>
<?
                }
                phpcounter_box_end();
        }
}

function phpcounter_box_begin($title="")
{
        global $color_table_border, $color_table_contents, $width_table, $font;
?>
<div align="center">
  <center>
  <table border="0" width="<?=$width_table?>" bgcolor="<?=$color_table_border?>" cellpadding="2" cellspacing="1">
<?if($title!=""){?>
    <tr>
      <td width="100%"><?=$font?><b><?=$title?></b></font></td>
    </tr>
<?}?>
    <tr>
      <td width="100%" bgcolor="<?=$color_table_contents?>">
        <table border="0" width="100%" cellspacing="2" cellpadding="2">
<?
}

function phpcounter_box_end()
{
?>
        </table>
      </td>
    </tr>
  </table>
  </center>
</div>
<br>
<?
}

function split_langs($str)
{
        $array = array();
        $lang_tags = explode(",", $str);
        foreach($lang_tags as $lang_tag)
        {
                if(trim($lang_tag)=="") continue;
                list($lang_full, $q) = explode(";", $lang_tag);
                $lang_full = trim($lang_full);
                list($lang, $sub_lang) = explode("-", $lang_full);
                $lang = strtolower(trim($lang));
                if($lang=="") continue;

                $array[$lang_full] = $lang;
        }
        return $array;
}

function phpcounter_brief_text($text, $max_len)
{
        if(strlen($text)<=$max_len) return $text;
        return substr($text, 0, $max_len)." ...";
}

function phpcounter_external_url($url)
{
        if(strncasecmp($url,"http://",7)!=0) return "http://$url";
        return $url;
}

function phpcounter_pages_line($action, $page, $pagesize, $numrows)
{
        global $name, $action, $l;
        $pages = (int)($numrows/$pagesize);
        if($numrows%$pagesize!=0) $pages++;
        if($page<0) $page = $pages-1;
        $start = $page-10;
        $end = $page+10;
        if($start<0) {$start=0;$end=$start+21;}
        if($end>$pages-1) {$end=$pages-1;$start=$end-20;if($start<0) $start=0;}
        ?>Pages: <?
        if($start>0) {?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=0">1</a> ... <?}
        for($i=$start; $i<=$end; $i++)
                if($i==$page) {?><b><?=($i+1)?></b> <?}
                else {?><a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=<?=$i?>"><?=($i+1)?></a> <?}
        if($end<$pages-1) {?>... <a href="index.php?l=<?=$l?>&amp;name=<?=$name?>&amp;action=<?=$action?>&amp;page=<?=$pages-1?>"><?=$pages?></a><?}
}

function seek($rs, $page, $pagesize)
{
        $numrows = mysql_num_rows($rs);
        $pages = (int)($numrows/$pagesize);
        if($numrows%$pagesize!=0) $pages++;
        if($page<0) $page = $pages-1;
        if($numrows>0) mysql_data_seek($rs, $page*$pagesize);
}

function html_code($name)
{
        global $counter_url;
        $code = join("", file("phpcounter.htm"));
        $code = str_replace("::URL::", $counter_url, $code);
        $code = str_replace("::NAME::", $name, $code);
        return $code;
}

function limit($page, $pagesize)
{
        if($pagesize<=0) return "";
        if($page<0) $page = 0;
        return " limit ".($page*$pagesize).", ".(int)$pagesize;
}
?>
Return current item: PHP Counter