17

track

track
SYNOPSIS

track($request_uri=false, $track_agent=false)

DESCRIPTION
CODE
  1. require_once 'clientipaddress.php';
  2. require_once 'validateipaddress.php';
  3. require_once 'requesturi.php';
  4. require_once 'useragent.php';
  5. require_once 'validateuseragent.php';

Loads the functions client_ip_address, validate_ip_address, request_uri, user_agent and validate_user_agent.

  1. function track($request_uri=false, $track_agent=false) {
  2.     global $track_log, $track_db;
  3.     global $track_agent_blacklist;
  4.  
  5.     if (! ($track_log or $track_db) ) {
  6.         return true;
  7.     }
  8.  
  9.     if (!$request_uri) {
  10.         $request_uri=request_uri();
  11.     }
  12.  
  13.     if (!$request_uri) {
  14.         return false;
  15.     }

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.

  1.     $user_agent=false;
  2.  
  3.     if ($track_agent or $track_agent_blacklist) {
  4.         $user_agent=user_agent();
  5.         if (!validate_user_agent($user_agent)) {
  6.             $user_agent=false;
  7.         }
  8.  
  9.         if ($user_agent and $track_agent_blacklist) {
  10.             $reg = '/' . implode('|', $track_agent_blacklist) . '/i';
  11.  
  12.             if (preg_match($reg, $user_agent)) {
  13.                 return true;
  14.             }
  15.         }
  16.     }

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.

  1.     $r = true;
  2.  
  3.     if ($track_log) {
  4.         require_once 'log.php';
  5.  
  6.         $logmsg = $request_uri;
  7.         if ($user_agent) {
  8.             $logmsg .= "\t" . $user_agent;
  9.         }
  10.  
  11.         $r = write_log($track_log === true ? 'track.log' : $track_log, $logmsg);
  12.  
  13.         if (!$r) {
  14.             return false;
  15.         }
  16.     }

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.

  1.     if ($track_db) {
  2.         $ip_address=client_ip_address();
  3.  
  4.         if (!validate_ip_address($ip_address)) {
  5.             return false;
  6.         }
  7.  
  8.         $sqlipaddress=db_sql_arg($ip_address, false);
  9.         $sqlrequesturi=db_sql_arg($request_uri, true);
  10.         $sqluseragent=db_sql_arg($user_agent, true, true);
  11.  
  12.         $tabtrack=db_prefix_table($track_db === true ? 'track' : $track_db);
  13.  
  14.         $sql="INSERT INTO $tabtrack (ip_address, request_uri, user_agent) VALUES (INET_ATON($sqlipaddress), $sqlrequesturi, $sqluseragent)";
  15.  
  16.         $r = db_insert($sql);
  17.  
  18.         if (!$r) {
  19.             return false;
  20.         }
  21.     }
  22.  
  23.     return true;
  24. }

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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].