<?php

require("dbconfig.php");

function 
query($querydata)
         {
     
$result = @mysql_query($querydata);
     if (
mysql_errno()!=0)
        echo(
"Eroare la interogare MySQL: ".mysql_error());
     return 
$result;
     }

$conn = @mysql_connect($db_host$db_user$db_pass) or die("Eroare la conectare MySQL: ".mysql_error());
mysql_select_db($db) or die("Eroare la selectarea bazei de date: " mysql_error());

function 
counts(){
$result=query("select count(*) as cnt from stattable");
$row mysql_fetch_array($result);
$total_counted $row['cnt'];
echo 
"Numar total de vizite: <b>".$total_counted."</b><br>";

$result=query("select count(distinct(remote_ip)) as cnt from stattable");
$row mysql_fetch_array($result);
$unique_counted $row['cnt'];
echo 
"Numar unic de vizitatori: <b>".$unique_counted."</b><br>";
}

function 
browser($decimals=1$width1=250$width2=60){
//sistemele de operare ale vizitatorilor

 
$unknown 0;
        
$result query("select remote_user_agent from stattable");
        while(
$row mysql_fetch_array($result))
                {
                list(,
$btype,) = explode(";"$row["remote_user_agent"]);
                
$btype trim($btype);
                if(
stristr($btype'msie') || $btype=='U' || stristr($btype'konq'))
                        {
                        if(
stristr($btype'msie')) $btype str_replace('MSIE''Internet Explorer'$btype);
                        if(
stristr($btype'konq')) $btype str_replace('/'' '$btype);
                        if(
$btype=='U'$btype 'Netscape / Gecko';
                        
$browser[$btype]++;
                        }
                else
                        
$unknown++;
                }

        
$sum $unknown;
        
$max $unknown;
        while(list(
$id,$count) = each($browser))
                {
                
$sum += $count;
                if(
$max $count$max $count;
                }
        if(
$sum==0$sum 1;
        if(
$max==0$max 1;

        
reset($browser);
        
arsort($browser);
        echo 
"<table>\n";
        echo 
"<table>\n";
        echo 
"<tr><td width=".$width1."></td><td width=".$width2."></td><td width=".$width2."></td><td></td></tr>\n";
        while(list(
$name,$count) = each($browser))
                {
                
$barwidth number_format($count $max 3000);
                
$percentage number_format($count $sum 100$decimals);
                echo 
"<tr><td>" $name "</td><td>" $count "</td><td align=\"right\">" $percentage "%&nbsp;&nbsp;</td><td><img height=10 width=" $barwidth " src=\"bar.gif\"></td></tr>\n";
                }
        if(
$unknown)
                {
                
$barwidth number_format($unknown $max 3000);
                
$percentage number_format($unknown $sum 1001);
                echo 
"<tr><td>unknown</td><td>{$unknown}</td><td align='right'>{$percentage}%&nbsp;&nbsp;</td><td><img height=10 width={$barwidth} src='bar.gif'></td></tr>\n";
                }
        echo 
"</table>\n";

}


function 
so($decimals=1$width1=250$width2=60){
        
query("select id from stattable");
        
$totalrows mysql_affected_rows();
        if(
$totalrows==0$totalrows 1;
        
$unknown 0;
        
$result query("select remote_user_agent from stattable");
        while(
$row mysql_fetch_array($result))
                {
                list(,,
$btype,) = str_replace (")"""explode(";"$row["remote_user_agent"]));
                if(
stristr($btype'win') || stristr($btype'mac') || stristr($btype'linux'))
                        {
                        
$platform[$btype]++;
                        }
                else
                        
$unknown++;
                }

        
$max $unknown;
        while(list(
$id,$count) = each($platform)) if($max $count$max $count;
        if(
$max==0$max 1;

        
arsort($platform);
        
reset($platform);
        
$total 0;
        echo 
"<table>\n";
        echo 
"<tr><td width=".$width1."></td><td width=".$width2."></td><td width=".$width2."></td><td></td></tr>\n";
        while(list(
$id,$count) = each($platform))
                {
                
$barwidth number_format($count $max 3000);
                
$percentage number_format($count $totalrows 1001);
                if(
$count!=0) echo "<tr><td>" $id "</td><td>" $count "</td><td align=\"right\">" $percentage "%&nbsp;&nbsp;</td><td><img height=10 width=" $barwidth " src=\"bar.gif\"></td></tr>\n";
                
$total++;
                }
        if(
$unknown)
                {
                
$barwidth number_format($unknown $max 3000);
                
$percentage number_format($unknown $totalrows 1001);
                echo 
"<tr><td>unknown</td><td>{$unknown}</td><td align='right'>{$percentage}%&nbsp;&nbsp;</td><td><img height=10 width={$barwidth} src='bar.gif'></td></tr>\n";
                }
        echo 
"</table>\n";

}

echo(
"<hr color=#000000 size='2' width='80%' align='left'>");
counts();
echo(
"<hr color=#000000 size='2' width='80%' align='left'>");
echo(
"<b>Tipul browserelor care au vizitat pagina: </b><br>");
browser(125060);
echo(
"<hr color=#000000 size='2' width='80%' align='left'>");
echo(
"<b>Sistemele de operare ale vizitatorilor: </b><br>");
so(1,250,60);
echo(
"<hr color=#000000 size='2' width='80%' align='left'>");
mysql_close($conn);

//show_source("dbconfig.php");
?>