TRACK YOUR VISITORS, USING PHP

There are some assorted reciprocation psychotherapy tools, ranging from
simple counters to rank reciprocation analyzers. Although there
are some liberated ones, most of them become with a toll tag. Why not
do it yourself? With PHP, you crapper easily create a index file
within minutes. In this article I power exhibit you how!

Getting the information The most essential part
is effort the aggregation from your visitor. Thankfully, this
is extremely cushy to do in PHP (or some another scripting language
for that matter). PHP has a primary orbicular uncertain called
$_SERVER which contains individual surround variables, including
information most your visitor. To intend every the aggregation you
want, only ingest the mass code:

 // Getting the
information $ipaddress = $_SERVER['REMOTE_ADDR']; $page =
"http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; $page .=
iif(!empty($_SERVER['QUERY_STRING']),
"?{$_SERVER['QUERY_STRING']}", ""); $referrer =
$_SERVER['HTTP_REFERER']; $datetime = mktime(); $useragent =
$_SERVER['HTTP_USER_AGENT']; $remotehost =
@getHostByAddr($ipaddress); 

As you crapper wager the majority
of aggregation comes from the $_SERVER variable. The mktime()
(http://nl2.php.net/mktime) and getHostByAddr()
(http://nl2.php.net/manual/en/function.gethostbyaddr.php)
functions are utilised to intend added aggregation most the
visitor.

Note: I utilised a duty in the above warning titled iif(). You
can intend this duty at http://www.phpit.ne
t/code/iif-function.

Logging the information Now that you hit all
the aggregation you need, it staleness be cursive to a index enter so
you crapper after countenance at it, and create multipurpose graphs and charts.
To do this you requirement a some ultimate PHP function, same fopen
(http://www.php.net/fopen) and fwrite
(http://www.php.net/fwrite).

The beneath cipher power prototypal create a rank distinction discover of every the
information. Then it power unstoppered the index enter in “Append” mode,
and if it doesn’t subsist yet, create it.

If no errors hit occurred, it power indite the newborn logline to the
log file, at the bottom, and eventually near the index enter again.

 // Create index distinction $logline = $ipaddress . '|' . $referrer
. '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' .
$page . " ";

// Write to index file: $logfile =
'/some/path/to/your/logfile.txt';

// Open the index enter in "Append" fashion if (!$handle =
fopen($logfile, 'a+')) { die("Failed to unstoppered index file"); }

// Write $logline to our logfile. if (fwrite($handle, $logline)
=== FALSE) { die("Failed to indite to index file"); }
fclose($handle); 

Now you’ve got a full duty logging
module. To move chase visitors on your website simply
include the logging power into your pages with the include()
function (http://www.php.net/include):

 include
('log.php'); 

Okay, today I poverty to analyse my log
file
After a patch you’ll belike poverty to analyse your
log file. You crapper easily do so by only using a accepted text
editor (like Notepad on Windows) to unstoppered the index file, but this
is farther from desired, because it’s in a hard-to-read format.

Let’s ingest PHP to create multipurpose overviews for is. The first
thing that needs to be finished is intend the plateau from the log
file in a variable, same so:

 // Open index enter $logfile =
"G:projectsphpitcontentrack your visitors using phplog.txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r"); $log = fread($handle,
filesize($logfile)); fclose($handle); } added { expire ("The log
file doesn't exist!"); } 

Now that the index enter is in a
variable, it’s prizewinning if apiece logline is in a removed variable.
We crapper do this using the explode() function
(http://www.php.net/explode), same so:

 // Seperate each
logline $log = explode(" ", trim($log)); 

After that it
may be multipurpose to intend apiece conception of apiece logline in a separate
variable. This crapper be finished by process finished apiece logline, and
using burst again:

 // Seperate apiece conception in apiece logline
for ($i = 0; $i < count($log); $i++) { $log[$i] =
trim($log[$i]); $log[$i] = explode('|', $log[$i]); } 

Now the rank index enter has been parsed, and we’re primed to
start generating some engrossing stuff.

The prototypal abstract that is rattling cushy to do is effort the sort of
pageviews. Simply ingest count() (http://www.phpit.net/count) on
the $log array, and there you hit it;

 reflexion count($log) .
" grouping hit visited this website."; 

You crapper also
generate a rank overview of your index file, using a simple
foreach wrap and tables. For example:

 // Show a plateau of
the logfile reflexion '<table>'; reflexion '<th>IP
Address</th>'; reflexion '<th>Referrer</th>'; echo
'<th>Date</th>'; echo
'<th>Useragent</th>'; reflexion '<th>Remote
Host</th>';

foreach ($log as $logline) { reflexion '<tr>';

echo '<td>' . $logline['0'] . '</td>'; echo
'<td>' . urldecode($logline['1']) . '</td>'; echo
'<td>' . date('d/m/Y', $logline['2']) . '</td>';
echo '<td>' . $logline['3'] . '</td>'; echo
'<td>' . $logline['4'] . '</td>';

echo '</tr>';

}

echo '</table>'; 

You crapper also ingest bespoken functions to separate discover see engines
and crawlers. Or create graphs using PHP/SWF Charts
(http://www.maani.us/charts/index.php). The possibilities are
endless, and you crapper do every kinds of things!

In Conclusion… In this article I hit shown
you hit to create a logging power for your possess PHP website,
using null more than PHP and its built-in functions. To view
the index enter you requirement to parse it using PHP, and then pass it
in some artefact you like. It is up to you to create a kick-ass
traffic analyzer.

If you ease favour to ingest a pre-built reciprocation analyzer, hit a
look at http://www.hotscripts.com.

Comments are closed.