Php 5 $ _SERVER ['SERVER_ADDR'] with multiple IPs

I am running ubuntu jaunty server with two network interfaces configured, one public IP, one private. When I ask for the server IP, I get the public IP. If I have multiple interfaces, is there a best practice for ensuring that I go public (which is what I want)?

<?php
echo " <table>";
echo "<tr><td>" .$_SERVER['SERVER_ADDR'] ."</td><td>SERVER_ADDR</td></tr>";
echo "<tr><td>" .$_SERVER['SERVER_NAME'] ."</td><td>SERVER_NAME</td></tr>";
echo " </table>";
?>

      

+2


source to share


3 answers


My impression is that you will get an address wherever the traffic comes from, so if you want to always act against an open interface, no matter where your request came from, you will have to ignore $_SERVER['SERVER_ADDR']

and define the IP you want to deal with in the code (hard-code it, parse the interface table and look for something that is not on the private network, what-have-you).



+1


source


I think it has something to do with Apache when setting up your domains. Apache recommends using a separate daemon on ip to keep them straight: http://httpd.apache.org/docs/1.3/vhosts/ip-based.html

Create a separate httpd installation for each virtual host. For each installation, use the Listen directive in the configuration file to select the IP address (or virtual host) that the daemons provide. eg.

`Listen www.smallco.com:80`

      

It is recommended to use the IP address instead of the hostname (see DNS Caveats ).



OR

Perhaps you can use your /etc/hosts

file to ensure that the hostname you choose always resolves the desired ip. See http://www.faqs.org/docs/securing/chap9sec95.html for information on /etc/hosts

.

0


source


You should always obtain a public IP address from public and private IP addresses from people on your private network. There is no sane way to ensure that you always get a public IP address, and it doesn't make sense

0


source







All Articles