You are here

public static function Cleantalk::cleantalk_get_real_ip in Anti Spam by CleanTalk 7.4

Same name and namespace in other branches
  1. 7.5 src/Cleantalk.php \Cleantalk::cleantalk_get_real_ip()
1 call to Cleantalk::cleantalk_get_real_ip()
Cleantalk::ct_session_ip in src/Cleantalk.php
Get user IP behind proxy server

File

src/Cleantalk.php, line 690

Class

Cleantalk
Cleantalk Base class

Code

public static function cleantalk_get_real_ip() {
  $headers = function_exists('apache_request_headers') ? apache_request_headers() : self::apache_request_headers();

  // Getting IP for validating
  if (array_key_exists('X-Forwarded-For', $headers)) {
    $ip = explode(",", trim($headers['X-Forwarded-For']));
    $ip = trim($ip[0]);
  }
  elseif (array_key_exists('HTTP_X_FORWARDED_FOR', $headers)) {
    $ip = explode(",", trim($headers['HTTP_X_FORWARDED_FOR']));
    $ip = trim($ip[0]);
  }
  else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }

  // Validating IP
  // IPv4
  if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    $the_ip = $ip;

    // IPv6
  }
  elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    $the_ip = $ip;

    // Unknown
  }
  else {
    $the_ip = null;
  }
  return $the_ip;
}