From 63e652caf1f1b1f10d04a05954d4ead245ba31ea Mon Sep 17 00:00:00 2001 From: JMcn <411164348@qq.com> Date: Sat, 12 Jan 2019 00:05:00 +0800 Subject: [PATCH] fix get ip in nginx reverse proxy --- device.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/device.php b/device.php index 85a561a..b98b4df 100644 --- a/device.php +++ b/device.php @@ -22,7 +22,7 @@ $D['version'] = '1.0.0'; $D['model'] = get_device_model(); $D['user'] = @get_current_user(); $D['hostname'] = gethostname(); -$D['hostip'] = ('/'==DIRECTORY_SEPARATOR) ? $_SERVER['SERVER_ADDR'] : @gethostbyname($_SERVER['SERVER_NAME']); +$D['hostip'] = get_device_ip(); $D['yourip'] = $_SERVER['REMOTE_ADDR']; $D['uname'] = @php_uname(); $D['os'] = explode(" ", php_uname()); @@ -56,6 +56,24 @@ else{ $D['cpu']['model'] = ''; } +function get_device_ip() { + $getIp = ('/'==DIRECTORY_SEPARATOR) ? $_SERVER['SERVER_ADDR'] : @gethostbyname($_SERVER['SERVER_NAME']); + if($getIp=='127.0.0.1'){ + $eth0_command="ifconfig eth0 | grep 'inet ' | cut -d: -f2 | awk '{ print $2}'"; + $eth0_ip = shell_exec($eth0_command); + if($eth0_ip==''){ + $wlan0_command="ifconfig wlan0 | grep 'inet ' | cut -d: -f2 | awk '{ print $2}'"; + $wlan0_ip = shell_exec($wlan0_command); + return $wlan0_ip; + } + else { + return $eth0_ip; + } + } else { + return $getIp; + } +} + function get_device_model(){ return ['name' => 'Raspberry Pi', 'id' => 'raspberry-pi']; }