You are here

function cleantalk_get_real_ip in Anti Spam by CleanTalk 8.3

1 call to cleantalk_get_real_ip()
Cleantalk::ct_session_ip in src/lib/Cleantalk/Antispam/Cleantalk.php
Get user IP behind proxy server

File

src/lib/Cleantalk/Antispam/Cleantalk.php, line 755

Namespace

Drupal\cleantalk\lib\Cleantalk\Antispam

Code

function cleantalk_get_real_ip() {

  // Getting headers
  $headers = function_exists('apache_request_headers') ? apache_request_headers() : $_SERVER;

  // 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;
}