October 13th, 2009
Change of appearance of the counter in CNStats
At installation CNStats in section ” the Configuration – the Code of the counter ” is offered to you for accommodation on a site two types of a code:
At installation CNStats in section ” the Configuration – the Code of the counter ” is offered to you for accommodation on a site two types of a code:
* An obligatory code of gathering of statistics of a site – he counts visitors of a site;
* opcional`nyj a code of display of the counter – this code simply draws a picture with figures of visitings – the graphic counter.
Appearance of the counter which displays the current visitings a site it is possible to change. In given clause{article} variants of graphic counter CNStats are considered{examined}.
By default, counter CNStats looks as follows:
On him three figures are displayed. The uppermost – it is hit everything, average – it is hit today, bottom – hosts today. In this note we shall consider ways of change of appearance of the counter and ways of display on him another, not less the helpful information.
At the end of a note you can see all kinds of considered{examined} counters and to download them for use.
The initial data
CNStats can give the following information for display to the counter:
* It is hit today, yesterday and all;
* Hosts today, yesterday and all;
* Users today, yesterday and all;
* Users now on a site.
As CNStats can takes into account visitings robots it is possible to deduce{remove} the information and on them:
* Robots today, yesterday and all;
* Percentage attitudes{relations} robots / users.
Besides it is possible to make various counters changes of attendance of a site graphically displaying dynamics{changes}.
Display of counters in CNStats
By default, the script displaying a picture of the counter is in CNStats root and called cnts.php (from English Counter-Show). I recommend to name files of your counters in a similar way, for example cnts-big.php, cnts-ttf.php, etc.
Let’s begin a spelling of a code with some, standard “fish” whom the code of any counter CNStats should contain:
<?
error_reporting (E_ALL and ~E_NOTICE);
// We are connected to a configuration file.
include “config.php”;
// We create images, from a preset pattern.
$im=ImageCreateFromPng (“button.png”);
// We are connected with MySql the server.
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONF ["sqlpassword"]);
if (mysql_errno () == 0) {
// We choose a database
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
//. Here all necessary actions are carried out
// On creation of the image…
}
}
// We send HTTP heading Content-Type
Header (” Content-type: image/png “);
// We generate images
ImagePng ($im);
// We clear memory
ImageDestroy ($im);
?>
Further this code will be inserted without comments.
The counter ¹1 – standard
<?
error_reporting (E_ALL and ~E_NOTICE);
include “config.php”;
$im=ImageCreateFromPng (“button.png”);
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONF ["sqlpassword"]);
if (mysql_errno () == 0) {
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
// We choose the data
// t_hits – it is hit all
// hits – it is hit today
// hosts – hosts today
$r = mysql_query (” SELECT t_hits, hits, hosts FROM cns_counter “);
// It is transferable{tolerable} the unique chosen recording in an associative file
$res = mysql_fetch_array ($r, MYSQL_ASSOC);
// We release{exempt} memory
@mysql_free_result ($r);
// Vydeljam necessary colors
$black=ImageColorAllocate ($im, 0,0,0);
$color=ImageColorAllocate ($im, $COUNTER ["inkR"],
$COUNTER ["inkG"],
$COUNTER ["inkB"]);
// We deduce{remove} numbers
ImageString ($im, 2,86-6*strlen ($res ["t_hits"]), 1, $res ["t_hits"], $color);
ImageString ($im, 1,85-5*strlen ($res ["hits"]), 13, $res ["hits"], $color);
ImageString ($im, 1,85-5*strlen ($res ["hosts"]), 20, $res ["hosts"], $color);
}
}
Header (” Content-type: image/png “);
ImagePng ($im);
ImageDestroy ($im);
?>
The most interesting part of this code is SQL search which chooses the displayed data from the table:
SELECT t_hits, hits, hosts FROM cns_counter
Let’s consider, that else we can “pull out” from tables CNStats so that it practically did not load the server.
SELECT hits, hosts, users,
t_hits, t_hosts, t_users,
u_hits, u_hosts,
u_t_hits, u_t_hosts
FROM cns_counter;
Here:
* hits – it is hit today
* hosts – hosts today
* users – users today
* t_hits – it is hit all
* t_hosts – hosts of all
* t_users – users of all
* u_hits – it is hit from visitors for today (robots are excluded)
* u_hosts – hosts from visitors for today (robots are excluded)
* u_t_hits – it is hit from visitors of all (robots are excluded)
* u_t_hosts – hosts from visitors of all (robots are excluded)
* u_hits-hits – it is hit from robots for today
* u_hosts-hosts – hosts from robots for today
* u_t_hits-t_hits – it is hit from robots of all
* u_t_hosts-t_hosts – hosts from robots of all
Remember! Robots are taken into account only at use “PHP-Include” and the “combined” counters.
The search which obtains quantity{amount} of visitors in the data the moment taking place on a site (conditionally) is below resulted.
SELECT count (DISTINCT hid) as online
FROM cns_log
WHERE date> NOW () – INTERVAL 3 MINUTE;
The counter ¹2 – is hit everything, hit today, users today and now on a site
As you see from the image of the counter, here we used other base image, for that what to find room for figure ” now on a site “. (red color)
<?
error_reporting (E_ALL and ~E_NOTICE);
include “config.php”;
$im=ImageCreateFromPng (“button-tt.png”);
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONF ["sqlpassword"]);
if (mysql_errno () == 0) {
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
// We choose the data
// t_hits – it is hit all
// hits – it is hit today
// users – users today
$r = mysql_query (” SELECT t_hits, hits, users FROM cns_counter “);
// It is transferable{tolerable} the unique chosen recording in an associative file
$res = mysql_fetch_array ($r, MYSQL_ASSOC);
// We release{exempt} memory
@mysql_free_result ($r);
// We select necessary colors
$black=ImageColorAllocate ($im, 0,0,0);
$red=ImageColorAllocate ($im, 255,0,0);
// We deduce{remove} numbers
ImageString ($im, 2,3,1, $res ["t_hits"], $black);
ImageString ($im, 1,3,13, $res ["hits"], $black);
ImageString ($im, 1,3,20, $res ["users"], $black);
// We choose ” now on a site ”
$r = mysql_query (” SELECT count (DISTINCT hid) as online
FROM cns_log
WHERE date> NOW () – INTERVAL 3 MINUTE; “);
$text = mysql_result ($r, 0,0);
@mysql_free_result ($r);
// We deduce{remove} ” now on a site ”
ImageString ($im, 1,80-5*strlen ($text), 4, $text, $red);
}
}
Header (” Content-type: image/png “);
ImagePng ($im);
ImageDestroy ($im);
?>
The counter ¹3 – use True-Type of fonts at generation of counters
Unfortunately, that support TrueType of fonts is established far from being on all servers, such counter is not included in the distribution kit. It is a pity, very beautiful counter
On this counter, for a change, we have deduced{removed} is hit today, hosts today and users today.
<?
error_reporting (E_ALL and ~E_NOTICE);
include “config.php”;
// This function allows to define{determine} length of a line in pixels
function width ($text) {
$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);
return ($box [2] – $box [1]);
}
// A font which we use for a conclusion of the counter.
// It is possible to take from C:WindowsFonts
define (‘ FONT_TAHOMA ‘,’ tahoma.ttf ‘);
// Attention! The image should be True-Color (24 bat)
$im=ImageCreateFromPng (“button-tt.png”);
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONF ["sqlpassword"]);
if (mysql_errno () == 0) {
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
// We choose the data
// hits – it is hit today
// hosts – hosts today
// users – users today
$r = mysql_query (” SELECT hits, hosts, users FROM cns_counter “);
// It is transferable{tolerable} the unique chosen recording in an associative file
$res = mysql_fetch_array ($r, MYSQL_ASSOC);
// We release{exempt} memory
@mysql_free_result ($r);
// We deduce{remove} the text
$text = $res ["hits"];
Imagettftext ($im, 7, 0, 4, 10, 0×03314A, FONT_TAHOMA, $text);
$text = $res ["hosts"];
Imagettftext ($im, 7, 0, 4, 19, 0×03314A, FONT_TAHOMA, $text);
$text = $res ["users"];
Imagettftext ($im, 7, 0, 4, 28, 0×03814A, FONT_TAHOMA, $text);
$r=mysql_query (” SELECT count (DISTINCT hid) as online
FROM cns_log
WHERE date> NOW () – INTERVAL 10 MINUTE; “);
$text = mysql_result ($r, 0,0);
Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xC0314A, FONT_TAHOMA, $text);
}
}
ImagePng ($im);
ImageDestroy ($im);
?>
The counter ¹4 – is hit everything, the percent{interest} of robots is hit today, users today,
In this example, besides robots we shall a little change appearance of the counter – we shall add shadows, and with the help of spaces we shall make numbers more legible.
<?
error_reporting (E_ALL and ~E_NOTICE);
include “config.php”;
// This function allows to define{determine} length of a line in pixels
function width ($text) {
$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);
return ($box [2] – $box [1]);
}
// A font which we use for a conclusion of the counter.
// It is possible to take from C:WindowsFonts
define (‘ FONT_TAHOMA ‘,’ tahoma.ttf ‘);
// Attention! The image should be True-Color (24 bat)
$im=ImageCreateFromPng (“button-tt.png”);
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONF ["sqlpassword"]);
if (mysql_errno () == 0) {
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
// We choose the data
// t_hits – it is hit all
// hits – it is hit today
// users – users today
// u_hits – it is hit from visitors
$r = mysql_query (” SELECT t_hits, hits, users, u_hits FROM cns_counter “);
// It is transferable{tolerable} the unique chosen recording in an associative file
$res = mysql_fetch_array ($r, MYSQL_ASSOC);
// We release{exempt} memory
@mysql_free_result ($r);
// We deduce{remove} the text
$text = number_format ($res ["t_hits"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 5, 11, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 4, 10, 0×03314A, FONT_TAHOMA, $text);
$text = number_format ($res ["hits"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 5, 20, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 4, 19, 0×04476C, FONT_TAHOMA, $text);
$text = number_format ($res ["users"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 5, 29, 0xcccccc, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 4, 28, 0×04876C, FONT_TAHOMA, $text);
// We consider percentage of robots
if ($res ["hits"]! =0)
$text = intval ((1-$ res ["u_hits"] / $res ["hits"]) *100). “%”;
else
$text = “0″;
// We deduce{remove} the text
Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 79-width ($text), 11, 0×03804A, FONT_TAHOMA, $text);
}
}
ImagePng ($im);
ImageDestroy ($im);
?>
The counter ¹5 – all together
This big counter is the sum of all previous.
<?
error_reporting (E_ALL and ~E_NOTICE);
include “config.php”;
// This function allows to define{determine} length of a line in pixels
function width ($text) {
$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);
return ($box [2] – $box [1]);
}
// A font which we use for a conclusion of the counter.
// It is possible to take from C:WindowsFonts
define (‘ FONT_TAHOMA ‘,’ tahoma.ttf ‘);
// Attention! The image should be True-Color (24 bat)
$im=ImageCreateFromPng (“button-big.png”);
$CONN = mysql_connect ($STATS_CONF ["sqlhost"],
$STATS_CONF ["sqluser"],
$STATS_CONFs ["sqlpassword"]);
if (mysql_errno () == 0) {
@mysql_select_db ($STATS_CONF ["dbname"]);
if (mysql_errno () == 0) {
$r = mysql_query (” SELECT t_hits, t_hosts, t_users, hits, hosts,
users, u_hits FROM cns_counter “);
$res = mysql_fetch_array ($r, MYSQL_ASSOC);
@mysql_free_result ($r);
$text = number_format ($res ["t_hits"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 11, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 10, 0×03314A, FONT_TAHOMA, $text);
$text = number_format ($res ["hits"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 20, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 19, 0×0331FF, FONT_TAHOMA, $text);
$text = number_format ($res ["t_hosts"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 31, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 30, 0×03314A, FONT_TAHOMA, $text);
$text = number_format ($res ["hosts"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 40, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 39, 0×0331FF, FONT_TAHOMA, $text);
$text = number_format ($res ["t_users"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 51, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 50, 0×03314A, FONT_TAHOMA, $text);
$text = number_format ($res ["users"], 0 “,”, ” “);
Imagettftext ($im, 7, 0, 86-width ($text), 60, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 59, 0×0331FF, FONT_TAHOMA, $text);
$r=mysql_query (” SELECT count (DISTINCT hid) as online
FROM cns_log
WHERE date> NOW () – INTERVAL 3 MINUTE; “);
$text = mysql_result ($r, 0,0);
Imagettftext ($im, 7, 0, 86-width ($text), 70, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 69, 0xC0314A, FONT_TAHOMA, $text);
$text = sprintf (” % .2f %% “, (1-$ res ["u_hits"] / $res ["hits"]) *100);
Imagettftext ($im, 7, 0, 86-width ($text), 82, 0xCCCCCC, FONT_TAHOMA, $text);
Imagettftext ($im, 7, 0, 85-width ($text), 81, 0×03804A, FONT_TAHOMA, $text);
}
}
ImagePng ($im);
ImageDestroy ($im);
?>