Database Reference
In-Depth Information
<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>
Although the
hitlog
table doesn't maintain page-access counts, you can determine
them easily:
• To determine the number of hits for a given page, use this statement:
SELECT
COUNT
(
*
)
FROM
hitlog
WHERE
path
=
'
path_name
'
;
• To determine the current counter value for all pages and retrieve them in order with
the most-requested pages first, do this:
SELECT
path
,
COUNT
(
*
)
FROM
hitlog
GROUP
BY
path
ORDER
BY
COUNT
(
*
)
DESC
;
20.14. Using MySQL for Apache Logging
Problem
You don't want to use MySQL to log accesses for just a few pages, as shown in
Recipe 20.13
. You want to log all page accesses, without having to put explicit logging
code in each page.
Solution
Tell Apache to log page accesses by writing to a MySQL table.
Discussion
The uses for MySQL in a web context aren't limited to page generation and processing.
MySQL can help you run the web server itself. For example, most Apache servers are
set up to log a record of page requests to a file. But it's also possible to send log records
to a program instead, from which you can write the records wherever you like—such
as to a database. Logging records in a database rather than a flat file makes the log more
highly structured and you can apply SQL analysis techniques to it. Want to see a par‐
ticular report? Write the SQL statements that produce it. To display the report in a