InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Apache tutorials

  • AWStats on Ubuntu
  • ApacheBench - load testing
  • Auth to a location or directory
  • Block directory access using htaccess 404
  • Different expire Headers for multiple images
  • Disable directory listing
  • List loaded modules
  • Log Content-Type in access log
  • Log latency and host in apache log
  • Monitoring using mod_status
  • Python to analyze bots in logs
  • Remove php extension from url
  • egrep and access log
  • log custom data in apache access log
  • mod_rewrite
  • top IP list from access log
 
  • Home
  • > Tutorials
  • > Web Development
  • > Apache

PHP – How to log custom data in apache access log

By admin on Jan 20, 2016

Apache lets you log a specific apache sub process environment variable in access log. PHP lets you set apache sub process env variable using apache_setenv. Here are the steps log custom data in apache access log when you are using Apache and PHP.

Log custom apache env variable in access log

Here is the syntax for logging env variable named mydata in access log:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{mydata}e\"" combined

Setting custom env variable from PHP

You can use the following code to set key mydata in apache subprocess env variable

<?php
apache_setenv("mydata", "somevalue1");
?>

In case you need to append something to custom data mydata, you can use
<?php
$current = apache_getenv("mydata");
$newval = $current."somevalue2";
apache_setenv("mydata", $newval);
?>

Possible use cases

Logging data in apache access log makes it easy to grep things from command line. It is easier to analyze apache access logs as everything is in one line. You can probably take a mixed strategy where you log verbose information in separate log files, and important and short information in apache access logs.

Suggested posts:

  1. Perl command line – replace multi line comments
  2. How to customize wordpress image alt tag
  3. Linux – Yesterday’s Date in YYYYMMDD format
  4. How to list git branches
  5. How to create simple animation using Inkscape and Jquery
  6. WordPress – how to add filter to description meta tag
  7. PHP sort associative array using custom compare function
  8. CSS – inline-block and baseline alignment
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Apache, PHP, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress