Recipe 8.16. Communicating Within Apache


8.16.1. Problem

You want to communicate from PHP to other parts of the Apache request process. This includes setting variables in the access_log.

8.16.2. Solution

Use apache_note( ) as shown in Example 8-39.

Communicating within Apache

<?php // get value $session = apache_note('session'); // set value apache_note('session', $session); ?>

8.16.3. Discussion

When Apache processes a request from a client, it goes through a series of steps; PHP plays only one part in the entire chain. Apache also remaps URLs, authenticates users, logs requests, and more. While processing a request, each handler has access to a set of key/value pairs called the notes table. The apache_note( ) function provides access to the notes table to retrieve information set by handlers earlier on in the process and leave information for handlers later on.

For example, if you use the session module to track users and preserve variables across requests, you can integrate this with your logfile analysis so you can determine the average number of page views per user. Use apache_note( ) in combination with the logging module to write the session ID directly to the access_log for each request. First, add the session ID to the notes table with the code in Example 8-40.

Adding the session ID to the notes table

<?php // retrieve the session ID and add it to Apache's notes table apache_note('session_id', session_id()); ?>

Then, modify your httpd.conf file to add the string %{session_id}n to your LogFormat. The trailing n tells Apache to use a variable stored in its notes table by another module.

If PHP is built with the --enable-memory-limit configuration option, it stores the peak memory usage of each request in a note called mod_php_memory_usage. Add the memory usage information to a LogFormat with %{mod_php_memory_usage}n.

8.16.4. See Also

Documentation on apache_note( ) at http://www.php.net/apache-note; information on logging in Apache at http://httpd.apache.org/docs/mod/mod_log_config.html.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net