Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      


Compile Apache Module - HTTP server extension - Add Server Variables to PHP

Adding a module to the Apache 2 HTTP web-server is actually pretty straightforward. In fact it is pretty similar to a Linux Kernel module.

This module will just add a variable to the HTTP request - you can read the added variables in e.g. PHP with the following PHP code:
<?
print $_SERVER{myvar};
?>
<hr>
<?
print apache_note("myvar");
?>


Compile the module:
gcc -fPIC -DSHARED_MODULE -I/usr/include/apache2 -I/usr/include/apr-0  -c mod_myvar.c
This will produce mod_myvar.o
Use the linker "ld" to make a shared object from the object file:
ld -Bshareable -o mod_myvar.so mod_myvar.o
Now copy the file to your apache modules directory:
cp mod_myvar.so /usr/lib/apache2/modules/
chmod 0755 /usr/lib/apache2/modules/mod_myvar.so



Now you need to make sure that Apache is loading the new module on startup.
So add this
LoadModule myvar_module /usr/lib/apache2/modules/mod_myvar.so
to your apache config, or create it as file in
/etc/apache2/mods-available/myvar.load
If you use the latter method, make sure you enable the module in mods-enabled:
# cd /etc/apache2/mods-enabled
# ln -s /etc/apache2/mods-available/myvar.load myvar.load

We're almost done! Just add the following stuff to your apache2.conf:
<IfModule mod_myvar.c>
  MyVarEnable On
</IfModule>
This will enable the new module.


mod_myvar.c
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_log.h"
#include "ap_config.h"

module AP_MODULE_DECLARE_DATA myvar_module;

static apr_status_t myvar_cleanup(void *cfgdata) {
   // cleanup code here, if needed
   return APR_SUCCESS;
}

static void myvar_child_init(apr_pool_t *p, server_rec *s) {
   apr_pool_cleanup_register(p, NULL, myvar_cleanup, myvar_cleanup);
}

static int myvar_post_read_request(request_rec *r) {
   char myvar[16] = "CAPTAIN WAS HERE";
   apr_table_set(r->notes, "myvar", myvar); // PHP: apache_notes
   apr_table_set( r->subprocess_env, "myvar", myvar ); // PHP: HTTP_SERVER_VARS
   return OK;
}

static const char *set_myvar_enable(cmd_parms *cmd, void *dummy, int arg) {
   ap_get_module_config(cmd->server->module_config, &myvar_module);
   return NULL;
}

static const command_rec myvar_cmds[] = {
   AP_INIT_FLAG( "MyVarEnable", set_myvar_enable, NULL, OR_FILEINFO, "Turn on mod_myvar"),
   {NULL}
};

static void myvar_register_hooks(apr_pool_t *p) {
   ap_hook_post_read_request( myvar_post_read_request, NULL, NULL, APR_HOOK_MIDDLE );
   ap_hook_child_init(        myvar_child_init,        NULL, NULL, APR_HOOK_MIDDLE );
}

// API hooks
module AP_MODULE_DECLARE_DATA myvar_module = {
   STANDARD20_MODULE_STUFF, 
   NULL,                        /* create per-dir    config structures */
   NULL,                        /* merge  per-dir    config structures */
   NULL,                        /* create per-server config structures */
   NULL,                        /* merge  per-server config structures */
   myvar_cmds,                  /* table of config file commands       */
   myvar_register_hooks         /* register hooks                      */
};


Last-Modified: Sat, 04 Feb 2006 16:03:19 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top