- Home /
How do you access the Ip Address in the Web Player?
I am having trouble getting the Ip Address in the web player.
I've tried 2 different methods,
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
return addr[addr.Length-1].ToString();
and also
Network.player.externalIP or Network.player.ipAddress
None of these seem to work due to the sandbox.
What is the officially recommended way to access this information?
Answer by GravityGamesDan · Jul 30, 2013 at 07:17 PM
So I managed to get it working, but I feel like it was through a dirty hack. I still feel like there should be some inside unity way to get the IP Address and I haven't found it yet. That being said, here is my current solution:
put a crossdomain.xml file on the server hosting the file
<?xml version="1.0" ?>
<cross-domain-policy>
<allow-access-from domain="*.EnterYourOwnDomainHere.com" />
</cross-domain-policy>
put a getIp.php file (which gets the IP address and echos it) on the server hosting the file
<?php
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
echo $ipaddress;
?>
Then, inside unity I make a string to hold the ip address. Then I had to use this IEnumerator thing to fill it with a WWW object:
IEnumerator startIPwww() {
WWW www = new WWW("http://www.EnterYourOwnDomainHere.com/getIP.php");
yield return www;
ipAddress = www.text;
}
Then in the start method of the script where I want to use the IP I placed:
StartCoroutine_Auto(startIPwww());
I feel like getting the IP address in the web based unity player should not be nearly so complicated for the end users of unity. Does anyone know a smoother way to do this so future searchers can just call a quickie function and get the IP without getting sandbox errors?
Actually this is the only way. The PC that runs the Webplayer doesn't know the external IP since it belongs to the router. The IP get exchanged when a packet passes through the router. There's no other way except sending something out to deter$$anonymous$$e if you are behind a router.
What you can do is deter$$anonymous$$e the IP address in the PHP page that contains your webplayer and pass the IP right to the unityplayer url. So inside Unity you can simply read Application.absoluteURL and extract the IP. In your PHP file use something like that inyour embed url (I actually don't like / use PHP so the syntax might vary):
http://yourdomain/yourpath/game.unity3d?ClientIP=$ipaddress
Your answer
