Php obtains the local ip address (remote ip address)
For example, php obtains the user IP address.
- // 111111111111
- Echo $ _ SERVER ['remote _ ADDR '];
-
- // 2222222222222
- Function get_local_ip (){
- $ Preg = "/\ A ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]) \.) {3} ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]) \ Z /";
- // Obtain the real IP address of the local machine whose operating system is win2000/xp or win7
- Exec ("ipconfig", $ out, $ stats );
- If (! Emptyempty ($ out )){
- Foreach ($ out AS $ row ){
- If (strstr ($ row, "IP") & strstr ($ row ,":")&&! Strstr ($ row, "IPv6 ")){
- $ TmpIp = explode (":", $ row );
- If (preg_match ($ preg, trim ($ tmpIp [1]) {
- Return trim ($ tmpIp [1]);
- }
- }
- } Bbs.it-home.org
- }
- // Obtain the real IP address of the local machine whose operating system is linux
- Exec ("ifconfig", $ out, $ stats );
- If (! Emptyempty ($ out )){
- If (isset ($ out [1]) & strstr ($ out [1], 'addr :')){
- $ TmpArray = explode (":", $ out [1]);
- $ TmpIp = explode ("", $ tmpArray [1]);
- If (preg_match ($ preg, trim ($ tmpIp [0]) {
- Return trim ($ tmpIp [0]);
- }
- }
- }
- Return '2014. 0.0.1 ';
- }
|