方法一:
if
(
'/'
==DIRECTORY_SEPARATOR){
$server_ip
=
$_SERVER
[
'SERVER_ADDR'
];
}
else
{
$server_ip
=@
gethostbyname
(
$_SERVER
[
'SERVER_NAME'
]);
}
echo
$server_ip
;
方法二:
/**
* 獲取服務(wù)器端IP地址
* @return string
*/
function
get_server_ip(){
if
(isset(
$_SERVER
)){
if
(
$_SERVER
[
'SERVER_ADDR'
]){
$server_ip
=
$_SERVER
[
'SERVER_ADDR'
];
}
else
{
$server_ip
=
$_SERVER
[
'LOCAL_ADDR'
];
}
}
else
{
$server_ip
=
getenv
(
'SERVER_ADDR'
);
}
return
$server_ip
;
}
echo
get_server_ip();