This is borderline programming, but i'm posting here first.
Output of PHP info
apache2handler
Apache Version Apache/2.2.3 (CentOS)
Apache API Version 20051115
Server Administrator root@localhost
Hostname:Port localdomain.com:0
User/Group apacheuser(500)/500
I'm trying to find a way for PHP to retrieve the "Hostname:port" field in this area.
We have apache that dumps all our virtual host information into a single file, and then that single file is broken up into several files & relevant information (total hits, ect) is logged to a database. One of the problems we're having at the moment is if a domain comes in on a ServerAlias, our php software isn't able to find the database table containing their logs (because the domain is technically not correct.) However, the "Hostname:port" field in php / phpinfo(); Always retrieves the information i want, which is the primary domain name.
If there is any other method PHP can gain the "ServerName" field from apache that i'm unaware of it, please let me know as that would be a lot easier.
An Alternative approach is having a SetEnv command in every virtual host, however instead of having to go through and add a
SetEnv primarydomain localdomain.com
to every VirtualHost, is there a way to use a variable?
In Example:
SetEnv primarydomain %v
Thanks!!
---------------------update---------------------------
We solved the problem with a regular expression search & replace.
Search: ^ServerName\s\b((?=[a-z0-9-]{1,63}.)[a-z0-9]+(-[a-z0-9]+)*.)+([a-z]{2,63})\b
Replace: $&\nSetEnv\tprimarydomain\t$1$3
If the standard $_SERVER['SERVER_NAME'] isnt waht you are after, have you tried $_SERVER['SERVER_SIGNATURE']?
http://uk.php.net/manual/en/reserved.variables.server.php
Do you set the ServerAdmin directive in the virtual host?
If so, I'd use
$_SERVER['SERVER_ADMIN']
, and take the latter part of the e-mail address after the @ symbol.Example:
Problem solved with a regexp.
Search: ^ServerName\s\b((?=[a-z0-9-]{1,63}.)[a-z0-9]+(-[a-z0-9]+)*.)+([a-z]{2,63})\b
Replace: $&\nSetEnv\tprimarydomain\t$1$3
If you're not interested in the client supplied hostnames then you might wish to use the directives:
UseCanonicalName and UseCanonicalPhysicalPort.
For the real server name (from the OS, not the virtual server) :
For the port :