|
|
|
| Title : |
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). |
| Categories : |
HTTP |
 Cary Collett |
| Date : |
Jan 17th 1999 |
| Grade : |
3 of 5 (graded 2 times) |
| Viewed : |
3328 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Cary Collett |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?php
/*
Pagestats v1.0
Copyright 1998 Cary Collett. Use it freely, distribute it freely, tell me
how great it is freely and send me any cool mods you come up with for it,
freely.
cary@ratatosk.org
Changes
1.1 Now using SCRIPT_FILENAME everywhere instead of SCRIPT_NAME. Using
SCRIPT_NAME could result in collisions if virtual hosts are being used.
I.e. hits for www.foo.com/index.html and www.bar.com/index.html would
both be collected for the same URI. Of course this makes the column
name URI a misnomer. Change it if it really bothers you. ;-)
*/
/*
A simple script to be included that reports hits, last modification
and when the hit counting started. You'll need MySQL support.
Though it should be easy to port this to any decent DBMS (mSQL
might be a little sticky becuase of it's severely limited
feature set; I don't use it anymore so I don't know for sure.)
This mysql table must exist in the database 'php' (or you can edit
stuff to suit you).
create table pagestats (
uri varchar(96) primary key,
hits bigint not null default 0,
since datetime
);
It defines several variables:
$hits = Number of hits counted
$since = full blown date & time since the counter started
$lastmod = full blown date & time of the last modification (ctime) of
the file
The following Unix epochs are set. Users can use these if they don't like
default format.
$sincesecs = Unix epoch of since
$modsecs = Unix epoch of lastmod
An example of usage:
1. Put this file somewhere in your PHP include path.
2. Stick something like this in your page:
<? include("pagestats.php3"); ?>
<? echo $hits . " hits since " . date('F d, Y',$sincesecs); ?>.
<p>
Last modified: <? echo $lastmod; ?>
<p>
*/
$link = mysql_pconnect( 'localhost', 'logger', '');
mysql_select_db( 'php',$link);
$qs = "UPDATE pagestats SET hits=hits+1 WHERE uri = '$SCRIPT_FILENAME'";
$r = mysql_query($qs,$link);
if (mysql_affected_rows($link) < 1) {
/* oops, no row for this page, create one */
mysql_query( "INSERT INTO pagestats VALUES ('$SCRIPT_FILENAME',1,SYSDATE())",$link);
}
$res = mysql_query( "SELECT hits, UNIX_TIMESTAMP(since) as since FROM pagestats WHERE uri = '$SCRIPT_FILENAME'",$link);
$sincesecs = mysql_result($res,0, 'since');
$modsecs = filectime($SCRIPT_FILENAME);
$hits = mysql_result($res,0, 'hits') + 1;
/*
see the date() docs if you want to change this
The format 'l F d, Y h:i:s A' makes dates like this:
Friday May 08, 1998 04:22:08 PM
*/
$dateformat = 'l F d, Y h:i:s A';
$since = date($dateformat,$sincesecs);
$lastmod = date($dateformat,$modsecs);
?>
|
|
| AJAX XML HTTP Request - Compatible with almost browsers! Categories : AJAX, HTTP, Java Script | | | header -- Send a raw HTTP header Categories : PHP, PHP Functions, HTTP | | | Remote File Saving with PHP - Download and serve a remote file. The content of the file will be updated at fixed intervals. Categories : PHP, Filesystem, Cache, Sockets, HTTP | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | A function to check if a URL exists Categories : PHP, CURL, HTTP | | | PHP4 HTTP Compression Speeds up the Web Categories : PHP, Zlib, HTML and PHP, HTTP, Network | | | WebServerSpy checks which kind of Webserver is running, Apache, Netscape, Fasttrack, IIS, HTTP-Header, HTTP 1.0, GET, spy, WWW Categories : HTTP, Network, Apache, PHP, Web Servers | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | Authentication HTTP protocol POST Categories : Authentication, HTTP, PHP | | | AITSH Errorbot Categories : Complete Programs, HTML and PHP, HTTP | | | Remote File Size Categories : PHP, Filesystem, HTTP, Sockets | | | Show hide table rows to make dynamic forms Categories : Beginner Guides, Java Script, Form Processing, HTTP | | | This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { } Categories : HTTP, Arrays, PHP | | | How to force the user to download a file instead of opening it up in an
controlled environment within the browser (i.e. MS Word/Adobe Acrobat) Categories : Browsers, PHP, HTTP | | | This script shows you the 7th latest php items from the mailing list archive on zend.com Categories : HTML, HTML and PHP, HTTP, PHP | |
|
|
|