home *** CD-ROM | disk | FTP | other *** search
- mSQL Page Log
-
- Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
-
-
-
- <?php
- /* PHP3/mSQL Page Log v1.0 by Rob Fisher
- 4 June 1998
-
- This script is provided with no guarantees that it will either
- work properly or not trash your machine. So don't blame me if
- it does. It works great for me though, and you can do whatever
- you want to it in the name of free software.
-
- It's not very general and will need adapting to work on your
- site. That's because I pulled it straight out of my web directory,
- annotated it, and stuck it on PX. If you need help, you can
- mail me. (Details below.) It would be nice to hear from anyone
- who uses this code, so why not sign my guestbook at
- http://helios.hud.ac.uk/web/ ?
-
- I wrote this script because I wanted to move from PHP/FI 2.0.1
- to PHP3, but I didn't want to lose the page access logging
- facility built into the former package.
-
- I also wished to make the changeover as smooth as possible,
- without starting counting hits from zero. To do this, I could
- have written a script to use and add to the existing PHPFI
- database on my system, but I decided to log only the information
- I wanted to use, and to take the final state of my PHP/FI logs
- as the starting point for my PHP3 ones.
-
- This script is intended to be include()d at the end of a page,
- just before the </BODY> tag. It appends a footer stating your
- name, the number of hits to the page, the time of the last hit
- and the date when the page was last updated. It also creates
- a link to the W3C HTML validator, so you are always only one
- click away from validating any page on your site.
-
- To use my script, you'll need to create a php3 database using the
- command "msqladmin create php3". (No quotes.) then create a table
- with the following commands:
-
-
- CREATE TABLE log (
- fname CHAR(128),
- hits INT,
- last_hit INT
- )\g
-
- CREATE UNIQUE INDEX idx ON log(fname)
- \g
-
- As you can see, this is a much more spare form of access logging
- than that built into PHP/FI, recording only the page name, the
- amount of times that page has been hit, and the date and time of
- the last hit. I don't need the extra information that PHP/FI logs,
- and keeping it builds up huge mSQL tables. If you want to record
- more data, it's not difficult to change this script.
-
- If you don't want to lose those hits you've worked so hard
- to acquire, when this script checks for hits to a page and finds
- none, it looks to see if an old PHP/FI access log exists for that
- page, and, if so, takes the number of hits and the last access
- from the correct table, moving that data into its own database.
-
- If you're going to check for old PHP/FI access logs, you need
- to assign the variable $UID to the UID you gave when initialising
- the phpfi database. If you don't want this check to be carried
- out, set the $check variable = false or remove the appropriate code */
-
- $check = true;
- $UID = 264;
-
- /* Set up a few other variables just to keep things clear */
-
- $author = "Rob Fisher"; /* your name here! */
- $mail = "r.d.fisher@hud.ac.uk"; /* your e-mail address here */
- $server = "helios.hud.ac.uk"; /* your server URL here */
- $host = helios; /* the machine running msql2d */
- $root = "/spare/apache/htdocs"; /* your server's document root */
- $name = $SCRIPT_FILENAME; /* the page including this file */
- $now = mktime(); /* the present time */
-
-
- /* Is there an mSQL server in the house? */
-
- if (msql_connect($host) == false):
- echo "No mSQL server - page logging disabled";
- exit();
- endif;
-
- /* We are going to need to truncate the path of the page which
- is including this script for the footer, and also to refer to the
- PHP/FI database. (PHP/FI used paths beginning at the server's
- document root.) */
-
- $shortname = ereg_replace( "$root", "", $SCRIPT_FILENAME);
-
- /* So, try to get the row for this page from the php3 database */
-
- $result = @msql( "php3", "SELECT * FROM log WHERE fname='$name'");
-
- if (@msql_numrows($result) == 0):
-
- /* Oh no! This page isn't in the database. (i.e. it hasn't
- had any hits to date.) Has the script been configured to
- look for old PHP/FI logs?*/
-
- if ($check == true):
-
- /* Yes. Try to retrieve the correct record */
-
- $result = msql( "phpfi", "SELECT total, timestamp FROM last$UID WHERE filename='$shortname'");
-
- if (@msql_numrows($result) > 0):
- $count = msql_result($result, 0, "total");
- $last_hit = msql_result($result, 0, "timestamp");
- else:
- /* No old PHP/FI records exist for this page,
- but we need some default values (hits = 1,
- last hit = present time) to put into the
- database.*/
-
- $count = 1;
- $last_hit = $now;
- endif;
-
- /* Now we have the number of hits and the last hit
- so we can put them in the database */
-
- msql( "php3", "INSERT INTO log VALUES ('$name', 1, $now)");
-
- else:
- /* We aren't taking the hits from a PHP/FI database
- but we still need the default values */
-
- $count = 1;
- $last_hit = $now;
-
- /* Now we have the number of hits and the last hit
- so we can put them in the database */
-
- msql( "php3", "INSERT INTO log VALUES ('$name', 1, $now)");
- endif;
-
- else:
- /* We have a php3 database record for this page, so retrieve
- the number of hits and the last hit from it. */
-
- $count = msql_result($result, 0, 'hits');
- $last_hit = msql_result($result, 0, 'last_hit');
- endif;
-
- /* The way I print my logging information. Change this to suit the
- rest of your site. Clicking on the link submits your page to the W3
- on-line validator. */
-
- echo "<HR><TABLE><TR><TD><A HREF=\"http://validator.w3.org/?url=http://";
- echo $server;
- 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";
- echo "<TD><SMALL>This page created and maintained by $author (<A HREF=\"mailto:$mail\">$mail</A>).<BR>\r";
- echo "It was last updated on ";
- print(date( "l F dS", filemtime($SCRIPT_FILENAME)));
- echo ", and has been hit $count times to date, the last occassion being on ";
- print(date( "l F dS", $last_hit));
-
- /* I only print the date, but you can easily print the time as well. */
-
- echo ".</SMALL></TD></TR></TABLE>";
-
- /* Increment the number of hits (to include this one) and write the
- new number back into the database. */
-
- $count++;
-
- msql( "php3", "UPDATE log SET hits=$count, last_hit=$now WHERE fname='$name'");
-
- ?>
-