Problem Solution Discussion Web Page Access Logging

The recipes dist ribut ion includes dem onst rat ion script s hit count er script s for Perl, PHP, and Pyt hon under t he apache direct ory. A JSP version is under t he t om cat direct ory. I nst all any of t hese in your web t ree, invoke it a few t im es, and wat ch t he count increase. First youll need t o creat e t he hitcount t able, as well as t he hitlog t able described in Recipe 18.14 . Bot h t ables can be creat ed from t he hit s.sql script provided in t he t ables direct ory.

18.14 Web Page Access Logging

18.14.1 Problem

You want t o know m ore about a page t han j ust t he num ber of t im es it s been accessed, such as t he t im e of access and t he host from which t he request originat ed.

18.14.2 Solution

Maint ain a hit log rat her t han a sim ple count er.

18.14.3 Discussion

The hitcount t able records only t he count for each page regist ered in it . I f you want t o record ot her inform at ion about page access, use a different approach. Suppose you want t o t rack t he client host and t im e of access for each request . I n t his case, you need a log for each page rat her t han j ust a count . But you can st ill m aint ain t he count s by using a m ult iple- colum n index t hat com bines t he page pat h and an AUTO_INCREMENT sequence colum n: CREATE TABLE hitlog path VARCHAR255 BINARY NOT NULL, hits BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, t TIMESTAMP, host VARCHAR64, PRIMARY KEY path,hits ; To insert new records, use t his query: INSERT INTO hitlog path, host VALUES path_val , host_val ; For exam ple, in a JSP page, hit s can be logged like t his: c:set var=host = request.getRemoteHost c:set c:if test={empty host} c:set var=host = request.getRemoteAddr c:set c:if c:if test={empty host} c:set var=host UNKNOWN c:set c:if sql:update dataSource={conn} INSERT INTO hitlog path, host VALUES?,? sql:param= request.getRequestURI sql:param sql:param value={host} sql:update The hitlog t able has t he following useful propert ies: • Access t im es are recorded aut om at ically in t he TIMESTAMP colum n t w hen you insert new records. • By linking t he path colum n t o an AUTO_INCREMENT colum n hits , t he count er values for a given page pat h increm ent aut om at ically whenever you insert a new record for t hat pat h. The count ers are m aint ained separat ely for each dist inct path value. For m ore inform at ion on how m ult iple-colum n sequences work, see Recipe 11.15 . • Theres no need t o check whet her t he count er for a page already exist s, because you insert a new row each t im e you record a hit for a page, not j ust for t he first hit . • I f you want t o det erm ine t he current count ers for each page, select t he record for each dist inct path value t hat has t he largest hits value: SELECT path, MAXhits FROM hitlog GROUP BY path;

18.15 Using MySQL for Apache Logging