How can I detect if the user is on localhost in PHP?
As a complement, as a function...
function isLocalhost($whitelist = ['127.0.0.1', '::1']) {
return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
}
You can also use $_SERVER['REMOTE_ADDR']
for which IP address of the client requesting is given by the web server.
$whitelist = array(
'127.0.0.1',
'::1'
);
if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
// not valid
}
Newer OS users (Win 7, 8) may also find it necessary to include an IPV6-format remote address in their whitelist array:
$whitelist = array('127.0.0.1', "::1");
if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
// not valid
}