track
track
SYNOPSIS
track($request_uri=false, $track_agent=false)
DESCRIPTION
CODE
- require_once 'clientipaddress.php';
- require_once 'validateipaddress.php';
- require_once 'requesturi.php';
- require_once 'useragent.php';
- require_once 'validateuseragent.php';
Loads the functions client_ip_address
, validate_ip_address
, request_uri
, user_agent
and validate_user_agent
.
- function track($request_uri=false, $track_agent=false) {
- global $track_log, $track_db;
- global $track_agent_blacklist;
- if (! ($track_log or $track_db) ) {
- return true;
- }
- if (!$request_uri) {
- $request_uri=request_uri();
- }
- if (!$request_uri) {
- return false;
- }
track
returns immediately if the global variables $track_log
and $track_db
are false
.
If the parameter $request_uri
isn't defined, track
initializes it by calling the function request_uri
.
If $request_uri
is still not defined, track
exits.
- $user_agent=false;
- if ($track_agent or $track_agent_blacklist) {
- $user_agent=user_agent();
- if (!validate_user_agent($user_agent)) {
- $user_agent=false;
- }
- if ($user_agent and $track_agent_blacklist) {
- $reg = '/' . implode('|', $track_agent_blacklist) . '/i';
- if (preg_match($reg, $user_agent)) {
- return true;
- }
- }
- }
If the parameter $track_agent
is true
or if the parameter $track_agent_blacklist
is set, the variable $user_agent
is initialized by calling the function user_agent
and validated.
If $user_agent
is defined and if its value matches one of the signatures listed in $track_agent_blacklist
, track
exits.
- $r = true;
- if ($track_log) {
- require_once 'log.php';
- $logmsg = $request_uri;
- if ($user_agent) {
- $logmsg .= "\t" . $user_agent;
- }
- $r = write_log($track_log === true ? 'track.log' : $track_log, $logmsg);
- if (!$r) {
- return false;
- }
- }
If the global variable $track_log
is true
, track
loads the function write_log
and asks it to log the request and possibly the agent in the file whose name is defined by $track_log
or called track.log by default.
- if ($track_db) {
- $ip_address=client_ip_address();
- if (!validate_ip_address($ip_address)) {
- return false;
- }
- $sqlipaddress=db_sql_arg($ip_address, false);
- $sqlrequesturi=db_sql_arg($request_uri, true);
- $sqluseragent=db_sql_arg($user_agent, true, true);
- $tabtrack=db_prefix_table($track_db === true ? 'track' : $track_db);
- $sql="INSERT INTO $tabtrack (ip_address, request_uri, user_agent) VALUES (INET_ATON($sqlipaddress), $sqlrequesturi, $sqluseragent)";
- $r = db_insert($sql);
- if (!$r) {
- return false;
- }
- }
- return true;
- }
If the global variable $track_db
is true
, track
obtains the IP address of the client and validates it then it prepares and executes an SQL order which registers the parameters of the request in the table whose name is defined by $track_db
or called track
by default.
USAGE
To display the content of the connection log:
$ tail track.log
To obtain the total number of visitors:
$ cut -f 1 track.log | cut -d' ' -f 3 | sort | uniq | wc -l
To list the 10 most consulted pages:
$ cut -f 2 track.log | sort | uniq -c | sort -rn | head -10
To check the DB:
mysql> SELECT * FROM track;
NOTE: If necessary, add the prefix $db_prefix
defined in db.inc to the name of the table track
.
To obtain the total number of visitors:
mysql> SELECT COUNT(DISTINCT ip_address) from track;
To list the 10 most consulted pages:
mysql> SELECT request_uri, COUNT(request_uri) AS count from track GROUP BY request_uri ORDER BY count DESC LIMIT 10;
IMPORTANT: The amount of data generated can rapidly fill up the DB and the log file. Choose only one mode by setting $track_db
or $track_log
to false
. Once a campaign for analyzing the types of the clients (navigators, mobiles, robots, etc.) is over, leave the parameter $track_agent
to false
.
SEE ALSO
engine, log, requesturi, useragent, validateuseragent, clientipaddress, validateipaddress
Comments