Kilho.net
IT

Getting the Access IP in AWS (PHP)

When using an Amazon server, retrieving the server IP returns the private IP address.

So I made this to get the public IP address.

<?
if(in_array(substr($_SERVER['SERVER_ADDR'], 0, 3), array('172')))
	if(strrpos(gethostname(), '.compute.internal')!==false)
		$_SERVER['SERVER_ADDR'] = file_get_contents('http://169.254.169.254/latest/meta-data/public-ipv4');

echo $_SERVER['SERVER_ADDR'];
?>

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *