home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / pagestats.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  3.0 KB  |  103 lines

  1. Pagestats 
  2.  
  3. A simple script to count and report hits and the last modification time of an HTML page. Requires MySQL support (other DBs should work too, except possibly mSQL). 
  4.  
  5.  
  6.  
  7. <?php 
  8. /* 
  9.   Pagestats v1.0 
  10.   
  11.   Copyright 1998 Cary Collett. Use it freely, distribute it freely, tell me 
  12.   how great it is freely and send me any cool mods you come up with for it, 
  13.   freely. 
  14.  
  15.   cary@ratatosk.org 
  16.  
  17.  
  18. Changes 
  19.  
  20. 1.1 Now using SCRIPT_FILENAME everywhere instead of SCRIPT_NAME. Using 
  21.     SCRIPT_NAME could result in collisions if virtual hosts are being used. 
  22.     I.e. hits for www.foo.com/index.html and www.bar.com/index.html would 
  23.     both be collected for the same URI. Of course this makes the column 
  24.     name URI a misnomer. Change it if it really bothers you. ;-) 
  25. */ 
  26. /* 
  27.  
  28.   A simple script to be included that reports hits, last modification 
  29.   and when the hit counting started. You'll need MySQL support.   
  30.   Though it should be easy to port this to any decent DBMS (mSQL 
  31.   might be a little sticky becuase of it's severely limited  
  32.   feature set; I don't use it anymore so I don't know for sure.) 
  33.    
  34.   This mysql table must exist in the database 'php' (or you can edit 
  35.   stuff to suit you). 
  36.    
  37.   create table pagestats ( 
  38.   uri varchar(96) primary key, 
  39.   hits bigint not null default 0, 
  40.   since datetime 
  41.   ); 
  42.    
  43.   It defines several variables: 
  44.    
  45.   $hits = Number of hits counted 
  46.   $since = full blown date & time since the counter started 
  47.   $lastmod = full blown date & time of the last modification (ctime) of  
  48.              the file 
  49.  
  50.   The following Unix epochs are set. Users can use these if they don't like 
  51.   default format. 
  52.  
  53.   $sincesecs = Unix epoch of since 
  54.   $modsecs = Unix epoch of lastmod 
  55.  
  56.   An example of usage: 
  57.  
  58.   1. Put this file somewhere in your PHP include path. 
  59.   2. Stick something like this in your page: 
  60.  
  61.  
  62. <? include("pagestats.php3"); ?> 
  63.  
  64. <? echo $hits . " hits since " . date('F d, Y',$sincesecs); ?>. 
  65. <p> 
  66. Last modified: <? echo $lastmod; ?> 
  67. <p> 
  68.  
  69.  
  70. */ 
  71.  
  72. $link = mysql_pconnect( 'localhost', 'logger', ''); 
  73. mysql_select_db( 'php',$link); 
  74.  
  75. $qs =  "UPDATE pagestats SET hits=hits+1 WHERE uri = '$SCRIPT_FILENAME'"; 
  76.  
  77. $r =  mysql_query($qs,$link); 
  78.  
  79. if (mysql_affected_rows($link) < 1) { 
  80.    /* oops, no row for this page, create one */ 
  81.   mysql_query( "INSERT INTO pagestats VALUES ('$SCRIPT_FILENAME',1,SYSDATE())",$link); 
  82.  
  83. $res = mysql_query( "SELECT hits, UNIX_TIMESTAMP(since) as since  FROM pagestats WHERE uri = '$SCRIPT_FILENAME'",$link); 
  84.  
  85. $sincesecs = mysql_result($res,0, 'since'); 
  86. $modsecs = filectime($SCRIPT_FILENAME); 
  87.  
  88. $hits    = mysql_result($res,0, 'hits') + 1; 
  89.  
  90. /*  
  91.    see the date() docs if you want to change this 
  92.    The format 'l F d, Y h:i:s A' makes dates like this:  
  93.    Friday May 08, 1998 04:22:08 PM 
  94. */ 
  95.  
  96. $dateformat =  'l F d, Y h:i:s A';  
  97.  
  98. $since   = date($dateformat,$sincesecs); 
  99. $lastmod = date($dateformat,$modsecs); 
  100. ?> 
  101.  
  102.