Forum Moderators: coopster

Message Too Old, No Replies

$ SERVER['SERVER ADDR'] blank

         

Nutter

8:02 pm on Jul 21, 2006 (gmt 0)

10+ Year Member



What would cause the SERVER_ADDR $_SERVER variable to be blank? It's only happening on one server running PHP 5.05 on FreeBSD.

And is there another way to get the IP of the server short of doing a DNS lookup on SERVER_NAME (which is there)?

jatar_k

2:04 am on Jul 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at the output of this and see if anything seems useful

echo '<pre>SERVER vars:<br>';
print_r($_SERVER);
echo '</pre>';

Nutter

2:57 am on Jul 22, 2006 (gmt 0)

10+ Year Member



Is that the same list that's under PHP Variables on phpinfo()? If so, the IP isn't listed there on this particular server.

I suppose the question is what setting would there be on the server that would cause this variable to be blank?

coopster

4:46 pm on Jul 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Which version of Apache?

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.

Nutter

10:45 pm on Jul 22, 2006 (gmt 0)

10+ Year Member



It's Apache 1.3.34, but I've got 1.3.38 running on my server and it works fine.

Check my thinking for a workaround please...

if ($_SERVER['SERVER_ADDR']!='')
{
$_SESSION['ip'] = $_SERVER['SERVER_ADDR'];
}
else
{
$_SESSION['ip'] = gethostbyname($_SERVER['SERVER_NAME']);
}

coopster

11:46 am on Jul 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, I stand corrected. It seems this particular variable,
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.



All that said then you have at least one issue with your snippet there yet -- you are going to have to test for the existence of the variable index as opposed to checking the value otherwise you are going to receive WARNING errors.
if (isset [php.net]($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] <> '') { 
$_SESSION['ip'] = $_SERVER['SERVER_ADDR'];
} else {
$_SESSION['ip'] = gethostbyname($_SERVER['SERVER_NAME']);
}

Other than that, I believe your logic should work for you.