Question

How can I access the unique_id that apache tracks for each request? I'd like to track it in any php scripts it calls as well as have it logged for each request.

I remove comment on the following line from http.conf file:

LoadModule unique_id_module libexec/apache2/mod_unique_id.so

and I am trying to reference the ID in this simple php script:

<?php

print("Hello world\n");
print($_SERVER['UNIQUE_ID']);
?>

Yet this script returns only Hello World.

Is this possible to do? I'm running macos if that makes any difference.

A var_dump of $_SERVER is as follows:

array(29) { ["HTTP_HOST"]=> string(9) "localhost" 
["HTTP_CONNECTION"]=> string(10) "keep-alive" 
["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" 
["HTTP_ACCEPT"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ["HTTP_USER_AGENT"]=> string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36" ["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip,deflate,sdch" 
["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-US,en;q=0.8" 
["HTTP_COOKIE"]=> string(11) "undefined=0" 
["PATH"]=> string(186) "/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/opt/local/lib/postgresql84/bin:/Applications/XAMPP/xamppfiles/bin" 
["SERVER_SIGNATURE"]=> string(0) "" 
    ["SERVER_SOFTWARE"]=> string(67) "Apache/2.2.24 (Unix) DAV/2 PHP/5.3.26 mod_ssl/2.2.24 OpenSSL/0.9.8y" 
    ["SERVER_NAME"]=> string(9) "localhost" 
    ["SERVER_ADDR"]=> string(3) "::1" 
    ["SERVER_PORT"]=> string(2) "80" 
    ["REMOTE_ADDR"]=> string(3) "::1" 
    ["DOCUMENT_ROOT"]=> string(28) "/Library/WebServer/Documents" 
    ["SERVER_ADMIN"]=> string(15) "you@example.com" 
    ["SCRIPT_FILENAME"]=> string(35) "/Library/WebServer/Documents/id.php" 
    ["REMOTE_PORT"]=> string(5) "49669" 
    ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" 
    ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" 
    ["REQUEST_METHOD"]=> string(3) "GET" 
    ["QUERY_STRING"]=> string(0) "" 
    ["REQUEST_URI"]=> string(7) "/id.php" 
    ["SCRIPT_NAME"]=> string(7) "/id.php" 
    ["PHP_SELF"]=> string(7) "/id.php" 
    ["REQUEST_TIME"]=> int(1397681759) 
    ["argv"]=> array(0) { } 
    ["argc"]=> int(0) }

Another problem I am having in the same area may point to the problem at hand: https://apple.stackexchange.com/questions/127801/where-are-the-apache-config-files-stored?noredirect=1#comment149749_127801

Était-ce utile?

La solution 5

I had to do a /usr/sbin/httpd -k stop and then a /usr/sbin/httpd -k start to get the config to take. Unsure why sudo apachectl restart LOOKED like it was restarting, but it wasn't....

Autres conseils

As mentioned in bounty comments, on OS X there are some issues regarding configuration files. For example, on OS X 10.7 I'm successfully enabled mod_unique_id in httpd.conf and if I make restart via Preference Pane - Web Sharing everything works as expected, but if I do restart via apachectl command some strange problems aries.

In OS X 10.9 there is no Web Sharing in Preference Pane so my advice is to stick to PHP's unique ID generator, for example those functions may help: http://www.php.net/manual/en/function.uniqid.php and http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php

In my fresh install of OS X 10.9.2, I have enabled unique_id_module and php5_module in /etc/apache2/httpd.conf, restart web server with sudo apachectl restart and it works correct.

I get unique_id_module listed when executing apachectl -M | sort and also got _SERVER["UNIQUE_ID"] in PHP script.

With script like:

<?php
echo $_SERVER["UNIQUE_ID"];
?>

I have output like this:

U08ZosCoAAMAAC9CATgAAAAA

Create a phpinfo.php script with following text inside:

<?php
phpinfo();
?>

After that - open it in the browser and see if unique_id_module is listed as 'loaded'.

As it said in module documentation:

The environment variable UNIQUE_ID is set to the identifier for each request

Try to use getenv('UNIQUE_ID') instead of $_SERVER['UNIQUE_ID'], its not the same

a2enmod unique_id

That worked for me after restarting Apache. I was getting a hyphen when using %{UNIQUE_ID}e with a custom LogFormat.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top