Php function to obtain the online ip address and client ip address

Source: Internet
Author: User
Tags get ip php example
Php function to obtain the online ip address and client ip address

  1. /**

  2. * Obtain the client ip address
  3. * @ Return [string] [description]
  4. */
  5. Function getclientip (){
  6. $ Ip = null;
  7. If (isset ($ _ server ['http _ x_forwarded_for ']) {
  8. $ Arr = explode (',', $ _ server ['http _ x_forwarded_for ']);
  9. $ Pos = array_search ('Unknown ', $ arr );
  10. If (false! ==$ Pos) unset ($ arr [$ pos]);
  11. $ Ip = trim ($ arr [0]);
  12. } Elseif (isset ($ _ server ['http _ client_ip ']) {
  13. $ Ip = $ _ server ['http _ client_ip '];
  14. } Elseif (isset ($ _ server ['remote _ addr ']) {
  15. $ Ip = $ _ server ['remote _ addr '];
  16. }
  17. // Valid IP Address Verification
  18. $ Ip = (false! = Ip2long ($ ip ))? $ Ip: '0. 0.0.0 ';
  19. Return $ ip;
  20. }

  21. /**

  22. * Getting an online ip address
  23. * @ Return string
  24. */
  25. Function getonlineip ($ format = 0 ){
  26. Global $ s_global;
  27. If (empty ($ s_global ['onlineip']) {
  28. If (getenv ('http _ client_ip ') & strcasecmp (getenv ('http _ client_ip'), 'Unknown ')){
  29. $ Onlineip = getenv ('http _ client_ip ');
  30. } Elseif (getenv ('http _ x_forwarded_for ') & strcasecmp (getenv ('http _ x_forwarded_for'), 'Unknown ')){
  31. $ Onlineip = getenv ('http _ x_forwarded_for ');
  32. } Elseif (getenv ('remote _ addr ') & strcasecmp (getenv ('remote _ addr'), 'Unknown ')){
  33. $ Onlineip = getenv ('remote _ addr ');
  34. } Elseif (isset ($ _ server ['remote _ addr ']) & $ _ server ['remote _ addr '] & strcasecmp ($ _ server ['remote _ addr'], 'Unknown ')){
  35. $ Onlineip = $ _ server ['remote _ addr '];
  36. }
  37. Preg_match ("/[\ d \.] {7, 15}/", $ onlineip, $ onlineipmatches );
  38. $ S_global ['onlineip'] = $ onlineipmatches [0]? $ Onlineipmatches [0]: 'Unknown ';
  39. }

  40. If ($ format ){

  41. $ Ips = explode ('.', $ s_global ['onlineip']);
  42. For ($ I = 0; $ I <3; $ I ++ ){
  43. $ Ips [$ I] = intval ($ ips [$ I]);
  44. }
  45. Return sprintf ('% 03d % 03d % 03d', $ ips [0], $ ips [1], $ ips [2]);
  46. } Else {
  47. Return $ s_global ['onlineip'];
  48. }
  49. }

Php: obtaining the real IP address of a remote client php method for obtaining the public ip address on an intranet machine php simple example for reading a pure IP database PHP method for obtaining the local LAN ip address PHP method for obtaining the computer name, IP address and MAC address on a LAN address PHP multiple ways to get IP addresses PHP code to get geographic location through IP address PHP get specified IP address segment information php IP get City API (pure IP database) php instance for obtaining real IP addresses: PHP method for obtaining domain names and domain name IP addresses php example for Obtaining geographic locations through IP address reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.