Forum Moderators: coopster
There is no guarantee that every webserver will provide any of the $_SERVER [php.net] array indices; servers may omit some, or provide others not listed in the manual pages. Like the manual says -- although "a large number of these variables are accounted for in the CGI 1.1 specification [hoohoo.ncsa.uiuc.edu], you should be able to expect those" -- it still depends on the server setup. You'll notice that SERVER_ADDR is not listed in the CGI specs. I don't believe SERVER_ADDR was available in Apache 1.x, as a matter of fact, it wasn't even available in the 2.x release until 2.0.43 -- based on a little hint in the Apache 2 SetEnvIf [httpd.apache.org] directive manual page:
Server_Addr - the IP address of the server on which the request was received (only with versions later than 2.0.43)
If this is your case and you need that environment variable I suppose you could always set your own as a workaround.
SERVER_ADDR, has indeed been around awhile it's just that it was a keyword-specific addition to the mod_setenvif module -- found it in the changelog for both 1.3.x and 2.0.x:
*) mod_setenvif: Add SERVER_ADDR special keyword to allow
envariable setting according to the server IP address
which received the request. [Ken Coar]
So I misinterpreted the docs earlier. I did indeed find some reference to SERVER_ADDR on the 1.3 mod_rewrite [httpd.apache.org] page. The Note on that page in that section is of particular interest:
Notice: These variables all correspond to the similarly named HTTP MIME-headers, C variables of the Apache server or struct tm fields of the Unix system. Most are documented elsewhere in the Manual or in the CGI specification. [...]
I know that of the virtual hosts running 1.3.x on which I have had access the SERVER_ADDR is not available. What I do not know is how this variable is either made available or made unavailable. I hope somebody else that comes along can share their understanding and/or insight. I haven't the time to dig in right now.
if (isset [php.net]($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] <> '') {
$_SESSION['ip'] = $_SERVER['SERVER_ADDR'];
} else {
$_SESSION['ip'] = gethostbyname($_SERVER['SERVER_NAME']);
}