| 1 | [[TracNav(TOC|nocollapse)]] |
| 2 | |
| 3 | = PHPMonitor = |
| 4 | |
| 5 | The PHPMonitor is a small PHP script with has to be pre-prended to all other PHP scripts of a website. This can be done either manually (unreliable) or by configuring the php.ini file of a server appropriatly (not always possible). The script relies on a cookie for the user identification to be available thorugh the [http://httpd.apache.org/docs/2.2/mod/mod_usertrack.html Apache module mod_usertrack]. |
| 6 | |
| 7 | {{{ |
| 8 | #!php |
| 9 | <?php |
| 10 | /* |
| 11 | * Usage monitoring script for PHP web applications. |
| 12 | * Requires the apache modules mod_usertrack to provide |
| 13 | * a cookie for user identification. |
| 14 | */ |
| 15 | $log_dir = dirname( __FILE__)."/"; |
| 16 | $log_name = "usage.log"; |
| 17 | $cookie_name = "swe_informatik_uni-goettingen_de"; |
| 18 | |
| 19 | $postkeys = ""; |
| 20 | |
| 21 | while (list($key, $value) = each($_POST)) { |
| 22 | $postkeys = $postkeys." ".$key; |
| 23 | } |
| 24 | if( $_SERVER['HTTP_REFERER']=='' ) { |
| 25 | $referer = '-'; |
| 26 | } else { |
| 27 | $referer = $_SERVER['HTTP_REFERER']; |
| 28 | } |
| 29 | |
| 30 | $cookieVal = $_COOKIE[$cookie_name]; |
| 31 | if($cookieVal==0) { |
| 32 | $apacheHeader = apache_response_headers(); |
| 33 | $explodeResult1 = explode(';',$apacheHeader['Set-Cookie']); |
| 34 | $explodeResult2 = explode('=',$explodeResult1[0]); |
| 35 | $cookieVal = $explodeResult2[1]; |
| 36 | } |
| 37 | |
| 38 | $log_entry = "\"".$cookieVal."\" \"".gmdate('Y-m-d H:i:s')."\" \"".$_SERVER['REQUEST_URI']."\" \"".$referer."\" \"".$_SERVER['HTTP_USER_AGENT']."\" \"".$postkeys."\"\r\n"; |
| 39 | $fp=fopen( $log_dir . $log_name, 'a' ); |
| 40 | fputs($fp, $log_entry); |
| 41 | fclose($fp); |
| 42 | ?> |
| 43 | }}} |
| 44 | |
| 45 | The resulting log files look as follows. |
| 46 | |
| 47 | {{{ |
| 48 | "172.20.0.9.1307611615198521" "2011-06-16 09:57:05" "/index.php?lang=de" "http://www.address.de/index.php?lang=de" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1)" "" |
| 49 | "172.20.0.9.1307611615198521" "2011-06-16 09:57:05" "/contentABC.php?lang=de" "http://www.address.de/index.php?lang=de" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1)" "postVar1" |
| 50 | "172.20.0.9.1307611615198521" "2011-06-16 09:57:05" "/contentABC.php?lang=de" "http://www.address.de/index.php?lang=de" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1)" "postVar1 postVar2" |
| 51 | "172.20.0.9.1307611615198521" "2011-06-16 09:57:05" "/contentXYZ.php?lang=de" "http://www.address.de/index.php?lang=de" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1)" "postVar2" |
| 52 | }}} |