Identity environment in PHP when script runs from CLI
I am using the correct configuration for each environment. To acomplissh, what should I identify with the HTTP_HOST variable, for example:
if($_SERVER['HTTP_HOST']=='example.com'){
the problem is when I run the code from the cli locally I can't figure out what the environment is.
I found a code that solves my problem
function getIPs($withV6 = true) {
preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
return $ips[1];
}
if($_SERVER['HTTP_HOST']=='example.com'||getIPs()[0]=='55.789.258.66'){
But ifconfig
only works on Linux, I detect which OS and change "ifconfig" to "ipconfig" (windows) using:
$getIpFunc = PHP_OS=='Linux'?`ifconfig`:`ipconfig`;
preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', $getIpFunc, $ips);
but i dont get ip in windows
+3
user7075468
source
to share
1 answer
You can use getHostByName
in both environments. It would probably be more reliable than using ipconfig or ifconfig.
$localIP = getHostByName(getHostName());
http://php.net/manual/en/function.gethostbyname.php http://php.net/manual/en/function.gethostname.php
+1
source to share