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

  1. mSQL Page Log 
  2.  
  3. Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0. 
  4.  
  5.  
  6.  
  7. <?php 
  8. /*    PHP3/mSQL Page Log v1.0 by Rob Fisher 
  9.     4 June 1998 
  10.  
  11.     This script is provided with no guarantees that it will either 
  12.     work properly or not trash your machine. So don't blame me if 
  13.     it does. It works great for me though, and you can do whatever 
  14.     you want to it in the name of free software. 
  15.  
  16.     It's not very general and will need adapting to work on your 
  17.     site. That's because I pulled it straight out of my web directory, 
  18.     annotated it, and stuck it on PX. If you need help, you can 
  19.     mail me. (Details below.) It would be nice to hear from anyone 
  20.     who uses this code, so why not sign my guestbook at 
  21.     http://helios.hud.ac.uk/web/ ? 
  22.  
  23.     I wrote this script because I wanted to move from PHP/FI 2.0.1 
  24.     to PHP3, but I didn't want to lose the page access logging 
  25.     facility built into the former package. 
  26.  
  27.     I also wished to make the changeover as smooth as possible, 
  28.     without starting counting hits from zero. To do this, I could 
  29.     have written a script to use and add to the existing PHPFI 
  30.     database on my system, but I decided to log only the information 
  31.     I wanted to use, and to take the final state of my PHP/FI logs 
  32.     as the starting point for my PHP3 ones. 
  33.  
  34.     This script is intended to be include()d at the end of a page, 
  35.     just before the </BODY> tag. It appends a footer stating your 
  36.     name, the number of hits to the page, the time of the last hit 
  37.     and the date when the page was last updated. It also creates 
  38.     a link to the W3C HTML validator, so you are always only one 
  39.     click away from validating any page on your site. 
  40.  
  41.     To use my script, you'll need to create a php3 database using the 
  42.     command "msqladmin create php3". (No quotes.) then create a table 
  43.     with the following commands: 
  44.  
  45.  
  46.         CREATE TABLE log ( 
  47.             fname    CHAR(128), 
  48.             hits    INT, 
  49.             last_hit    INT 
  50.         )\g 
  51.  
  52.         CREATE UNIQUE INDEX idx ON log(fname) 
  53.         \g 
  54.  
  55.     As you can see, this is a much more spare form of access logging 
  56.     than that built into PHP/FI, recording only the page name, the 
  57.     amount of times that page has been hit, and the date and time of 
  58.     the last hit. I don't need the extra information that PHP/FI logs, 
  59.     and keeping it builds up huge mSQL tables. If you want to record 
  60.     more data, it's not difficult to change this script. 
  61.  
  62.     If you don't want to lose those hits you've worked so hard 
  63.     to acquire, when this script checks for hits to a page and finds 
  64.     none, it looks to see if an old PHP/FI access log exists for that 
  65.     page, and, if so, takes the number of hits and the last access 
  66.     from the correct table, moving that data into its own database. 
  67.  
  68.     If you're going to check for old PHP/FI access logs, you need 
  69.     to assign the variable $UID to the UID you gave when initialising 
  70.     the phpfi database. If you don't want this check to be carried 
  71.     out, set the $check variable = false or remove the appropriate code */ 
  72.  
  73.     $check = true; 
  74.     $UID = 264; 
  75.  
  76.      /* Set up a few other variables just to keep things clear */ 
  77.  
  78.     $author =  "Rob Fisher";         /* your name here! */ 
  79.     $mail =  "r.d.fisher@hud.ac.uk";     /* your e-mail address here */ 
  80.     $server =  "helios.hud.ac.uk";     /* your server URL here */ 
  81.     $host = helios;             /* the machine running msql2d */ 
  82.     $root =  "/spare/apache/htdocs";  /* your server's document root */ 
  83.     $name = $SCRIPT_FILENAME;     /* the page including this file */ 
  84.     $now = mktime();         /* the present time */ 
  85.      
  86.  
  87.      /* Is there an mSQL server in the house? */ 
  88.  
  89.     if (msql_connect($host) == false): 
  90.         echo  "No mSQL server - page logging disabled"; 
  91.         exit(); 
  92.     endif; 
  93.  
  94.      /* We are going to need to truncate the path of the page which 
  95.     is including this script for the footer, and also to refer to the 
  96.     PHP/FI database. (PHP/FI used paths beginning at the server's 
  97.     document root.) */ 
  98.  
  99.     $shortname = ereg_replace( "$root",  "", $SCRIPT_FILENAME); 
  100.  
  101.      /* So, try to get the row for this page from the php3 database */ 
  102.  
  103.     $result = @msql( "php3",  "SELECT * FROM log WHERE fname='$name'"); 
  104.  
  105.     if (@msql_numrows($result) == 0): 
  106.      
  107.          /* Oh no! This page isn't in the database. (i.e. it hasn't 
  108.         had any hits to date.) Has the script been configured to 
  109.         look for old PHP/FI logs?*/ 
  110.  
  111.         if ($check == true): 
  112.  
  113.              /* Yes. Try to retrieve the correct record */ 
  114.  
  115.             $result = msql( "phpfi",  "SELECT total, timestamp FROM last$UID WHERE filename='$shortname'"); 
  116.  
  117.             if (@msql_numrows($result) > 0): 
  118.                 $count = msql_result($result, 0,  "total");  
  119.                        $last_hit = msql_result($result, 0,  "timestamp"); 
  120.             else: 
  121.                  /* No old PHP/FI records exist for this page, 
  122.                 but we need some default values (hits = 1, 
  123.                 last hit = present time) to put into the 
  124.                 database.*/ 
  125.  
  126.                 $count = 1; 
  127.                 $last_hit = $now; 
  128.             endif; 
  129.  
  130.              /* Now we have the number of hits and the last hit 
  131.             so we can put them in the database */ 
  132.  
  133.             msql( "php3",  "INSERT INTO log VALUES ('$name', 1, $now)"); 
  134.  
  135.         else: 
  136.              /* We aren't taking the hits from a PHP/FI database 
  137.             but we still need the default values */ 
  138.  
  139.             $count = 1; 
  140.             $last_hit = $now; 
  141.  
  142.              /* Now we have the number of hits and the last hit 
  143.             so we can put them in the database */ 
  144.  
  145.             msql( "php3",  "INSERT INTO log VALUES ('$name', 1, $now)"); 
  146.         endif; 
  147.  
  148.     else: 
  149.          /* We have a php3 database record for this page, so retrieve 
  150.         the number of hits and the last hit from it. */ 
  151.  
  152.         $count = msql_result($result, 0,  'hits'); 
  153.         $last_hit = msql_result($result, 0,  'last_hit'); 
  154.     endif; 
  155.  
  156.      /* The way I print my logging information. Change this to suit the 
  157.     rest of your site. Clicking on the link submits your page to the W3 
  158.     on-line validator. */ 
  159.  
  160.     echo  "<HR><TABLE><TR><TD><A HREF=\"http://validator.w3.org/?url=http://"; 
  161.     echo $server; 
  162.     echo  "$shortname\" TARGET=\"_top\"><IMG BORDER=0 SRC=\"http://validator.w3.org/images/vh40.gif\" ALT=\"Valid HTML 4.0!\" HEIGHT=31 WIDTH=88></A></TD>\r"; 
  163.     echo  "<TD><SMALL>This page created and maintained by $author (<A HREF=\"mailto:$mail\">$mail</A>).<BR>\r"; 
  164.     echo  "It was last updated on "; 
  165.     print(date( "l F dS", filemtime($SCRIPT_FILENAME))); 
  166.     echo  ", and has been hit $count times to date, the last occassion being on "; 
  167.     print(date( "l F dS", $last_hit)); 
  168.  
  169.      /* I only print the date, but you can easily print the time as well. */ 
  170.  
  171.     echo  ".</SMALL></TD></TR></TABLE>"; 
  172.  
  173.      /* Increment the number of hits (to include this one) and write the 
  174.     new number back into the database. */ 
  175.  
  176.     $count++; 
  177.  
  178.     msql( "php3",  "UPDATE log SET hits=$count, last_hit=$now WHERE fname='$name'"); 
  179.  
  180. ?> 
  181.