Gathering of statistics on PHP
Statistical data on visitors of a site bring not not enough advantage. On statistics it is possible to adjust design of a site according to the sanction of the majority of visitors, to adjust design to a browser on which come the most part of visitors and it is simply interesting, who looks on you on a site, from under what OC, and it can the search robot jandeksa or gugla? Though some systems of tracking visitors are cherezvychajno complex , but with the help of rather simple system it is possible to receive curious data on visitors of a site. I shall show as to make with a kind simple magazine of visitings of a site with help PHP and cookies (MySQL it is not required). Besides it is possible to expand my example easily.
For that the system would work, it is necessary to build a script of statistics in each page. Well or in those pages, which statistics of visitings you want to see. Our script will write down the following data:
? A browser + OC (HTTP_USER_AGENT)
? IP (REMOTE_ADDR address
? A host (REMOTE_HOST)
? Page - refferer (HTTP_REFERER)
? Time of visit (date (" d.m. Y H:i:s "))
? Required (REQUEST_URI address
Even these data, I think, will be rather interesting to web designers. So, we shall start. The script will be called sniffer.php. I shall result the text of all script and I shall add it to plentiful comments:
<? php
// sniffer.php
// Protection against direct start
// A script whom that the stranger
if (eregi ("sniffer.php", $PHP_SELF)) {
Header (" Location: index.php ");
die ();
}
extract ($HTTP_GET_VARS);
extract ($HTTP_POST_VARS);
extract ($HTTP_COOKIE_VARS);
extract ($HTTP_SERVER_VARS);
// This fragment of a code has been borrowed
// From system PHP Nuke;)
// Further I declare variables
$fileName="stat.txt "; // a name of a file with statistics
$maxVisitors=30; // quantity{amount} of the recordings displayed
// At viewing statistics
$cookieName = "visitorOfMySite"; // a name kuki
$cookieValue = "1"; // value kuki
$timeLimit=86400; // term in seconds which should
// To pass from the moment of last visiting a site, what
// The information on the visitor has entered the name repeatedly. It
// Value is equal to 1 day, i.e. the same visitor
// Enters the name in statistics of times in one day. If
// This variable to equate to zero will be taken into account
// All visitings the same visitor
// The variables responsible for display further follow
// Statistics
$headerColor = " * 808080 ";
$headerFontColor = " * FFFFFF ";
$fontFace = " Arial, Times New Roman, Verdana ";
$fontSize = "1";
$tableColor = " * 000000 ";
$rowColor = " * CECECE ";
$fontColor = " * 0000A0 ";
$textFontColor = " * 000000 ";
// All variables are prepared.
// Function of data recording about the visitor
function saveUserData () {
GLOBAL $fileName, $HTTP_USER_AGENT, $REMOTE_ADDR, $REMOTE_HOST,
$HTTP_REFERER, $REQUES_URI;
$curTime=date (" d.m. Y H:i:s "); // current time and date
// I prepare the data for recording
if (empty ($HTTP_USER_AGENT)) {$HTTP_USER_AGENT = "Unkwnown";}
if (empty ($REMOTE_ADDR)) {$REMOTE_ADDR = " Not Resolved ";}
if (empty ($REMOTE_HOST)) {$REMOTE_HOST = "Unknown";}
if (empty ($HTTP_REFERER)) {$HTTP_REFERER = " No Referer ";}
if (empty ($REQUEST_URI)) {$REQUEST_URI = "Unknown";}
$data_ = $HTTP_USER_AGENT. "::". $REMOTE_ADDR. "::". $REMOTE_HOST. "::
". $HTTP_REFERER. ":: ". $REQUEST_URI. ":: ". $curTime. "\r\n ";
// A separator will be two ":"
// Further I write to a file
if (is_writeable ($fileName)):
$fp = fopen ($fileName, "a");
fputs ($fp, $data _);
fclose ($fp);
endif;
}
// Function of recording is ready. Now it is necessary to write
// Function of a conclusion of the data from a file of statistics
function showStat () {
GLOBAL $headerColor, $headerFontColor, $fontFace, $fontSize, $tableColor,
$fileName, $maxVisitors, $rowColor, $fontColor, $textFontColor;
// I take out the table
$fbase=file ($fileName);
$fbase = array_reverse ($fbase);
$count = sizeOf ($fbase);
echo " <font face = \ " $ fontFace \ " color = \ " $ textFontColor \ " size = \ " $ fontSize \ ">";
echo " All visitings: $count <br> <br> ";
echo " <div align = \ " center \ ">
<table cellpadding = \ " 2 \ " cellspacing = \ " 1 \ " width = \ " 95 % \ "
border = \ " 0 \ " bgcolor = \ " $ tableColor \ "> ";
echo " <tr bgcolor = \ " $ headerColor \ "> <td> <
font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> the Browser
</font>
</td> <td> <font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> IP </font> </td>
<td> <font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> the Host </font> </td>
<td> <font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> the Link </font> </td>
<td> <font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> Page </font> </td>
<td> <font face = \ " $ fontFace \ " color = \ " $ headerFontColor \ "
size = \ " $ fontSize \ "> Time of visit </font> </td> </tr> ";
echo " </font> <font face = \ " $ fontFace \ " size = \ " $ fontSize \ ">";
// I open a file and I start a cycle
$fbase=file ($fileName);
$fbase = array_reverse ($fbase);
for ($i=0; $i <$maxVisitors; $i ++):
if ($i> = sizeof ($fbase)) {break;}
$s = $fbase [$i];
// I share
$strr = explode ("::", $s);
if (empty ($strr)) {break;}
// I take out the data
echo " <tr> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [0] </font>
</td> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [1] </font>
</td> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [2] </font>
</td> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [3] </font>
</td> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [4] </font>
</td> <td bgcolor = \ " $ rowColor \ "> <
font face = \ " $ fontFace \ " color = \ " $ fontColor \ "
size = \ " $ fontSize \ "> $ strr [5] </font> </td>
</tr> ";
endfor;
}
?>
The script of gathering and show of statistics is ready. Now it is necessary to insert into those pages, the information on which visitors you want to see{overlook}:
<? php
include ("sniffer.php");
if (! isset ($$ cookieName)):
// To establish kuki
setcookie ($cookieName, $cookieValue, time () + $timeLimit);
saveUserData ();
endif;
?>
Zdras`te! And mona you to count? It is possible? Well thanks! I have counted you!;)
Pay attention, that this code needs to be inserted into the top of page before the data will be passed in a browser. Otherwise to establish kuki will fail. Further we shall make page, vyvodjahhjuju statistics:
<html> <body>
<? php include ("sniffer.php");?>
Statistics <br>
<? php
showStat ();
?> </body> </html> </i>
Here we have simply switched on a file sniffer.php and have called from him function showStat () with the help of such small script, long is equal in 100 lines, it is possible to receive all with help PHP and in a convenient kind to see{overlook}. Still more many that is necessary to make, for example to make statistics on refereram, to browsers... As it is possible from HTTP_USER_AGENT to pull out a browser and OS and to write down them in more convenient kind. By the way, all the sizes long at a conclusion of statistics I raschityval at the sanction 1024*768 and at me all conveniently is located in one line.

© Web Development Company Conkurent, LLC 2007-2009. All rights reserved. |