This is borderline programming, but i'm posting here first.

output of PHPinfo

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 dump 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 retrives the information i want, which is the primary domain name.

If there is any other method PHP can gain the "ServerName" field from apache i'm unaware of please let me know, as that would be alot 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

link|improve this question

79% accept rate
feedback

4 Answers

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

link|improve this answer
Unfortunately both of those return the ServerAlias instead of ServerName. =/ – GruffTech Jul 23 '09 at 19:26
feedback

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:

array_pop(split("@", $_SERVER['SERVER_ADMIN'], 2))'
link|improve this answer
feedback
up vote 0 down vote accepted

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

link|improve this answer
Can you explain more how you did this? – Josh K May 1 '10 at 3:03
feedback

If you're not interested in the client supplied hostnames then you might wish to use the directives:

UseCanonicalName and UseCanonicalPhysicalPort.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.