Guest User

Untitled

a guest
Apr 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. function ip_address() {
  2. static $ip_address = NULL;
  3.  
  4. if (!isset($ip_address)) {
  5. $ip_address = $_SERVER['REMOTE_ADDR'];
  6.  
  7. // Only use parts of the X-Forwarded-For (XFF) header that have followed a trusted route.
  8. // Specifically, identify the leftmost IP address in the XFF header that is not one of ours.
  9. // An XFF header is: X-Forwarded-For: client1, proxy1, proxy2
  10. if (isset($_SERVER['HTTP_' . variable_get('x_forwarded_for_header', 'X_FORWARDED_FOR')]) && variable_get('reverse_proxy', 0)) {
  11. // Load trusted reverse proxy server IPs.
  12. $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
  13.  
  14. // Turn XFF header into an array.
  15. $forwarded = explode(',', $_SERVER['HTTP_' . variable_get('x_forwarded_for_header', 'X_FORWARDED_FOR')]);
  16.  
  17. // Trim the forwarded IPs; they may have been delimited by commas and spaces.
  18. $forwarded = array_map('trim', $forwarded);
  19.  
  20. // Tack direct client IP onto end of forwarded array.
  21. $forwarded[] = $ip_address;
  22.  
  23. // Eliminate all trusted IPs.
  24. $untrusted = array_diff($forwarded, $reverse_proxy_addresses);
  25.  
  26. // The right-most IP is the most specific we can trust.
  27. $ip_address = array_pop($untrusted);
  28. }
  29. }
  30.  
  31. return $ip_address;
  32. }
Add Comment
Please, Sign In to add comment