1. 首先要判断当前环境
1 function getIP() { 2 if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) { 3 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 4 } else if (@$_SERVER["HTTP_CLIENT_IP"]) { 5 $ip = $_SERVER["HTTP_CLIENT_IP"]; 6 } else if (@$_SERVER["REMOTE_ADDR"]) { 7 $ip = $_SERVER["REMOTE_ADDR"]; 8 } else if (@getenv("HTTP_X_FORWARDED_FOR")) { 9 $ip = getenv("HTTP_X_FORWARDED_FOR"); 10 } else if (@getenv("HTTP_CLIENT_IP")) { 11 $ip = getenv("HTTP_CLIENT_IP"); 12 } else if (@getenv("REMOTE_ADDR")) { 13 $ip = getenv("REMOTE_ADDR"); 14 } else { 15 $ip = "Unknown"; 16 } 17 18 return $ip; 19 }
2. if 条件判断并部署
1 $__DEV__ = false; 2 3 if( strpos($IP,‘192.168‘) !== false || strpos($IP,‘127.0.0.1‘) !== false) { //comment 4 $__DEV__ = true; 5 6 sleep(1); 7 } 8 9 if (!$__DEV__) { //comment 10 11 “线上!” 12 13 } else { 14 15 “线下!” 16 17 }