Apache - Php Server_Addr Showing Internal Ip?
Echo $_Server['Server_Addr'] Always Yields 192.168.1.142, Which Is the Ip Address of My Server Within My Home Network. I Can Access the Website Using My...
echo $_SERVER['SERVER_ADDR'] always yields 192.168.1.142, which is the IP address of my server within my home network. I can access the website using my external IP since I've set up the port forward on my router, but $_SERVER['SERVER_ADDR'] will only return the internal IP, no matter what I try.
How can I make $_SERVER['SERVER_ADDR'] return my external IP?
2 Answers
You can use file_get_contents function for this. You just need to find a host which can return your ip address
$externalContent = file_get_contents(');
preg_match('/Current IP Address: ([\[\]:.[0-9a-fA-F]+)</', $externalContent, $myIp);
echo $myexternalIp = $myIp[1];
Add a second IP to your server (ifconfig eth0:1 192.168.1.143) and use that in your router's port forwarding rules. That way, external accesses will be coming in through that address instead and you can tell the difference between the two.